l2tp_netlink.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  1. /*
  2. * L2TP netlink layer, for management
  3. *
  4. * Copyright (c) 2008,2009,2010 Katalix Systems Ltd
  5. *
  6. * Partly based on the IrDA nelink implementation
  7. * (see net/irda/irnetlink.c) which is:
  8. * Copyright (c) 2007 Samuel Ortiz <samuel@sortiz.org>
  9. * which is in turn partly based on the wireless netlink code:
  10. * Copyright 2006 Johannes Berg <johannes@sipsolutions.net>
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License version 2 as
  14. * published by the Free Software Foundation.
  15. */
  16. #include <net/sock.h>
  17. #include <net/genetlink.h>
  18. #include <net/udp.h>
  19. #include <linux/in.h>
  20. #include <linux/udp.h>
  21. #include <linux/socket.h>
  22. #include <linux/module.h>
  23. #include <linux/list.h>
  24. #include <net/net_namespace.h>
  25. #include <linux/l2tp.h>
  26. #include "l2tp_core.h"
  27. static struct genl_family l2tp_nl_family = {
  28. .id = GENL_ID_GENERATE,
  29. .name = L2TP_GENL_NAME,
  30. .version = L2TP_GENL_VERSION,
  31. .hdrsize = 0,
  32. .maxattr = L2TP_ATTR_MAX,
  33. };
  34. /* Accessed under genl lock */
  35. static const struct l2tp_nl_cmd_ops *l2tp_nl_cmd_ops[__L2TP_PWTYPE_MAX];
  36. static struct l2tp_session *l2tp_nl_session_find(struct genl_info *info)
  37. {
  38. u32 tunnel_id;
  39. u32 session_id;
  40. char *ifname;
  41. struct l2tp_tunnel *tunnel;
  42. struct l2tp_session *session = NULL;
  43. struct net *net = genl_info_net(info);
  44. if (info->attrs[L2TP_ATTR_IFNAME]) {
  45. ifname = nla_data(info->attrs[L2TP_ATTR_IFNAME]);
  46. session = l2tp_session_find_by_ifname(net, ifname);
  47. } else if ((info->attrs[L2TP_ATTR_SESSION_ID]) &&
  48. (info->attrs[L2TP_ATTR_CONN_ID])) {
  49. tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
  50. session_id = nla_get_u32(info->attrs[L2TP_ATTR_SESSION_ID]);
  51. tunnel = l2tp_tunnel_find(net, tunnel_id);
  52. if (tunnel)
  53. session = l2tp_session_find(net, tunnel, session_id);
  54. }
  55. return session;
  56. }
  57. static int l2tp_nl_cmd_noop(struct sk_buff *skb, struct genl_info *info)
  58. {
  59. struct sk_buff *msg;
  60. void *hdr;
  61. int ret = -ENOBUFS;
  62. msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  63. if (!msg) {
  64. ret = -ENOMEM;
  65. goto out;
  66. }
  67. hdr = genlmsg_put(msg, info->snd_pid, info->snd_seq,
  68. &l2tp_nl_family, 0, L2TP_CMD_NOOP);
  69. if (IS_ERR(hdr)) {
  70. ret = PTR_ERR(hdr);
  71. goto err_out;
  72. }
  73. genlmsg_end(msg, hdr);
  74. return genlmsg_unicast(genl_info_net(info), msg, info->snd_pid);
  75. err_out:
  76. nlmsg_free(msg);
  77. out:
  78. return ret;
  79. }
  80. static int l2tp_nl_cmd_tunnel_create(struct sk_buff *skb, struct genl_info *info)
  81. {
  82. u32 tunnel_id;
  83. u32 peer_tunnel_id;
  84. int proto_version;
  85. int fd;
  86. int ret = 0;
  87. struct l2tp_tunnel_cfg cfg = { 0, };
  88. struct l2tp_tunnel *tunnel;
  89. struct net *net = genl_info_net(info);
  90. if (!info->attrs[L2TP_ATTR_CONN_ID]) {
  91. ret = -EINVAL;
  92. goto out;
  93. }
  94. tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
  95. if (!info->attrs[L2TP_ATTR_PEER_CONN_ID]) {
  96. ret = -EINVAL;
  97. goto out;
  98. }
  99. peer_tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_PEER_CONN_ID]);
  100. if (!info->attrs[L2TP_ATTR_PROTO_VERSION]) {
  101. ret = -EINVAL;
  102. goto out;
  103. }
  104. proto_version = nla_get_u8(info->attrs[L2TP_ATTR_PROTO_VERSION]);
  105. if (!info->attrs[L2TP_ATTR_ENCAP_TYPE]) {
  106. ret = -EINVAL;
  107. goto out;
  108. }
  109. cfg.encap = nla_get_u16(info->attrs[L2TP_ATTR_ENCAP_TYPE]);
  110. fd = -1;
  111. if (info->attrs[L2TP_ATTR_FD]) {
  112. fd = nla_get_u32(info->attrs[L2TP_ATTR_FD]);
  113. } else {
  114. if (info->attrs[L2TP_ATTR_IP_SADDR])
  115. cfg.local_ip.s_addr = nla_get_be32(info->attrs[L2TP_ATTR_IP_SADDR]);
  116. if (info->attrs[L2TP_ATTR_IP_DADDR])
  117. cfg.peer_ip.s_addr = nla_get_be32(info->attrs[L2TP_ATTR_IP_DADDR]);
  118. if (info->attrs[L2TP_ATTR_UDP_SPORT])
  119. cfg.local_udp_port = nla_get_u16(info->attrs[L2TP_ATTR_UDP_SPORT]);
  120. if (info->attrs[L2TP_ATTR_UDP_DPORT])
  121. cfg.peer_udp_port = nla_get_u16(info->attrs[L2TP_ATTR_UDP_DPORT]);
  122. if (info->attrs[L2TP_ATTR_UDP_CSUM])
  123. cfg.use_udp_checksums = nla_get_flag(info->attrs[L2TP_ATTR_UDP_CSUM]);
  124. }
  125. if (info->attrs[L2TP_ATTR_DEBUG])
  126. cfg.debug = nla_get_u32(info->attrs[L2TP_ATTR_DEBUG]);
  127. tunnel = l2tp_tunnel_find(net, tunnel_id);
  128. if (tunnel != NULL) {
  129. ret = -EEXIST;
  130. goto out;
  131. }
  132. ret = -EINVAL;
  133. switch (cfg.encap) {
  134. case L2TP_ENCAPTYPE_UDP:
  135. case L2TP_ENCAPTYPE_IP:
  136. ret = l2tp_tunnel_create(net, fd, proto_version, tunnel_id,
  137. peer_tunnel_id, &cfg, &tunnel);
  138. break;
  139. }
  140. out:
  141. return ret;
  142. }
  143. static int l2tp_nl_cmd_tunnel_delete(struct sk_buff *skb, struct genl_info *info)
  144. {
  145. struct l2tp_tunnel *tunnel;
  146. u32 tunnel_id;
  147. int ret = 0;
  148. struct net *net = genl_info_net(info);
  149. if (!info->attrs[L2TP_ATTR_CONN_ID]) {
  150. ret = -EINVAL;
  151. goto out;
  152. }
  153. tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
  154. tunnel = l2tp_tunnel_find(net, tunnel_id);
  155. if (tunnel == NULL) {
  156. ret = -ENODEV;
  157. goto out;
  158. }
  159. (void) l2tp_tunnel_delete(tunnel);
  160. out:
  161. return ret;
  162. }
  163. static int l2tp_nl_cmd_tunnel_modify(struct sk_buff *skb, struct genl_info *info)
  164. {
  165. struct l2tp_tunnel *tunnel;
  166. u32 tunnel_id;
  167. int ret = 0;
  168. struct net *net = genl_info_net(info);
  169. if (!info->attrs[L2TP_ATTR_CONN_ID]) {
  170. ret = -EINVAL;
  171. goto out;
  172. }
  173. tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
  174. tunnel = l2tp_tunnel_find(net, tunnel_id);
  175. if (tunnel == NULL) {
  176. ret = -ENODEV;
  177. goto out;
  178. }
  179. if (info->attrs[L2TP_ATTR_DEBUG])
  180. tunnel->debug = nla_get_u32(info->attrs[L2TP_ATTR_DEBUG]);
  181. out:
  182. return ret;
  183. }
  184. static int l2tp_nl_tunnel_send(struct sk_buff *skb, u32 pid, u32 seq, int flags,
  185. struct l2tp_tunnel *tunnel)
  186. {
  187. void *hdr;
  188. struct nlattr *nest;
  189. struct sock *sk = NULL;
  190. struct inet_sock *inet;
  191. hdr = genlmsg_put(skb, pid, seq, &l2tp_nl_family, flags,
  192. L2TP_CMD_TUNNEL_GET);
  193. if (IS_ERR(hdr))
  194. return PTR_ERR(hdr);
  195. NLA_PUT_U8(skb, L2TP_ATTR_PROTO_VERSION, tunnel->version);
  196. NLA_PUT_U32(skb, L2TP_ATTR_CONN_ID, tunnel->tunnel_id);
  197. NLA_PUT_U32(skb, L2TP_ATTR_PEER_CONN_ID, tunnel->peer_tunnel_id);
  198. NLA_PUT_U32(skb, L2TP_ATTR_DEBUG, tunnel->debug);
  199. NLA_PUT_U16(skb, L2TP_ATTR_ENCAP_TYPE, tunnel->encap);
  200. nest = nla_nest_start(skb, L2TP_ATTR_STATS);
  201. if (nest == NULL)
  202. goto nla_put_failure;
  203. NLA_PUT_U64(skb, L2TP_ATTR_TX_PACKETS, tunnel->stats.tx_packets);
  204. NLA_PUT_U64(skb, L2TP_ATTR_TX_BYTES, tunnel->stats.tx_bytes);
  205. NLA_PUT_U64(skb, L2TP_ATTR_TX_ERRORS, tunnel->stats.tx_errors);
  206. NLA_PUT_U64(skb, L2TP_ATTR_RX_PACKETS, tunnel->stats.rx_packets);
  207. NLA_PUT_U64(skb, L2TP_ATTR_RX_BYTES, tunnel->stats.rx_bytes);
  208. NLA_PUT_U64(skb, L2TP_ATTR_RX_SEQ_DISCARDS, tunnel->stats.rx_seq_discards);
  209. NLA_PUT_U64(skb, L2TP_ATTR_RX_OOS_PACKETS, tunnel->stats.rx_oos_packets);
  210. NLA_PUT_U64(skb, L2TP_ATTR_RX_ERRORS, tunnel->stats.rx_errors);
  211. nla_nest_end(skb, nest);
  212. sk = tunnel->sock;
  213. if (!sk)
  214. goto out;
  215. inet = inet_sk(sk);
  216. switch (tunnel->encap) {
  217. case L2TP_ENCAPTYPE_UDP:
  218. NLA_PUT_U16(skb, L2TP_ATTR_UDP_SPORT, ntohs(inet->inet_sport));
  219. NLA_PUT_U16(skb, L2TP_ATTR_UDP_DPORT, ntohs(inet->inet_dport));
  220. NLA_PUT_U8(skb, L2TP_ATTR_UDP_CSUM, (sk->sk_no_check != UDP_CSUM_NOXMIT));
  221. /* NOBREAK */
  222. case L2TP_ENCAPTYPE_IP:
  223. NLA_PUT_BE32(skb, L2TP_ATTR_IP_SADDR, inet->inet_saddr);
  224. NLA_PUT_BE32(skb, L2TP_ATTR_IP_DADDR, inet->inet_daddr);
  225. break;
  226. }
  227. out:
  228. return genlmsg_end(skb, hdr);
  229. nla_put_failure:
  230. genlmsg_cancel(skb, hdr);
  231. return -1;
  232. }
  233. static int l2tp_nl_cmd_tunnel_get(struct sk_buff *skb, struct genl_info *info)
  234. {
  235. struct l2tp_tunnel *tunnel;
  236. struct sk_buff *msg;
  237. u32 tunnel_id;
  238. int ret = -ENOBUFS;
  239. struct net *net = genl_info_net(info);
  240. if (!info->attrs[L2TP_ATTR_CONN_ID]) {
  241. ret = -EINVAL;
  242. goto out;
  243. }
  244. tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
  245. tunnel = l2tp_tunnel_find(net, tunnel_id);
  246. if (tunnel == NULL) {
  247. ret = -ENODEV;
  248. goto out;
  249. }
  250. msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  251. if (!msg) {
  252. ret = -ENOMEM;
  253. goto out;
  254. }
  255. ret = l2tp_nl_tunnel_send(msg, info->snd_pid, info->snd_seq,
  256. NLM_F_ACK, tunnel);
  257. if (ret < 0)
  258. goto err_out;
  259. return genlmsg_unicast(net, msg, info->snd_pid);
  260. err_out:
  261. nlmsg_free(msg);
  262. out:
  263. return ret;
  264. }
  265. static int l2tp_nl_cmd_tunnel_dump(struct sk_buff *skb, struct netlink_callback *cb)
  266. {
  267. int ti = cb->args[0];
  268. struct l2tp_tunnel *tunnel;
  269. struct net *net = sock_net(skb->sk);
  270. for (;;) {
  271. tunnel = l2tp_tunnel_find_nth(net, ti);
  272. if (tunnel == NULL)
  273. goto out;
  274. if (l2tp_nl_tunnel_send(skb, NETLINK_CB(cb->skb).pid,
  275. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  276. tunnel) <= 0)
  277. goto out;
  278. ti++;
  279. }
  280. out:
  281. cb->args[0] = ti;
  282. return skb->len;
  283. }
  284. static int l2tp_nl_cmd_session_create(struct sk_buff *skb, struct genl_info *info)
  285. {
  286. u32 tunnel_id = 0;
  287. u32 session_id;
  288. u32 peer_session_id;
  289. int ret = 0;
  290. struct l2tp_tunnel *tunnel;
  291. struct l2tp_session *session;
  292. struct l2tp_session_cfg cfg = { 0, };
  293. struct net *net = genl_info_net(info);
  294. if (!info->attrs[L2TP_ATTR_CONN_ID]) {
  295. ret = -EINVAL;
  296. goto out;
  297. }
  298. tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
  299. tunnel = l2tp_tunnel_find(net, tunnel_id);
  300. if (!tunnel) {
  301. ret = -ENODEV;
  302. goto out;
  303. }
  304. if (!info->attrs[L2TP_ATTR_SESSION_ID]) {
  305. ret = -EINVAL;
  306. goto out;
  307. }
  308. session_id = nla_get_u32(info->attrs[L2TP_ATTR_SESSION_ID]);
  309. session = l2tp_session_find(net, tunnel, session_id);
  310. if (session) {
  311. ret = -EEXIST;
  312. goto out;
  313. }
  314. if (!info->attrs[L2TP_ATTR_PEER_SESSION_ID]) {
  315. ret = -EINVAL;
  316. goto out;
  317. }
  318. peer_session_id = nla_get_u32(info->attrs[L2TP_ATTR_PEER_SESSION_ID]);
  319. if (!info->attrs[L2TP_ATTR_PW_TYPE]) {
  320. ret = -EINVAL;
  321. goto out;
  322. }
  323. cfg.pw_type = nla_get_u16(info->attrs[L2TP_ATTR_PW_TYPE]);
  324. if (cfg.pw_type >= __L2TP_PWTYPE_MAX) {
  325. ret = -EINVAL;
  326. goto out;
  327. }
  328. if (tunnel->version > 2) {
  329. if (info->attrs[L2TP_ATTR_OFFSET])
  330. cfg.offset = nla_get_u16(info->attrs[L2TP_ATTR_OFFSET]);
  331. if (info->attrs[L2TP_ATTR_DATA_SEQ])
  332. cfg.data_seq = nla_get_u8(info->attrs[L2TP_ATTR_DATA_SEQ]);
  333. cfg.l2specific_type = L2TP_L2SPECTYPE_DEFAULT;
  334. if (info->attrs[L2TP_ATTR_L2SPEC_TYPE])
  335. cfg.l2specific_type = nla_get_u8(info->attrs[L2TP_ATTR_L2SPEC_TYPE]);
  336. cfg.l2specific_len = 4;
  337. if (info->attrs[L2TP_ATTR_L2SPEC_LEN])
  338. cfg.l2specific_len = nla_get_u8(info->attrs[L2TP_ATTR_L2SPEC_LEN]);
  339. if (info->attrs[L2TP_ATTR_COOKIE]) {
  340. u16 len = nla_len(info->attrs[L2TP_ATTR_COOKIE]);
  341. if (len > 8) {
  342. ret = -EINVAL;
  343. goto out;
  344. }
  345. cfg.cookie_len = len;
  346. memcpy(&cfg.cookie[0], nla_data(info->attrs[L2TP_ATTR_COOKIE]), len);
  347. }
  348. if (info->attrs[L2TP_ATTR_PEER_COOKIE]) {
  349. u16 len = nla_len(info->attrs[L2TP_ATTR_PEER_COOKIE]);
  350. if (len > 8) {
  351. ret = -EINVAL;
  352. goto out;
  353. }
  354. cfg.peer_cookie_len = len;
  355. memcpy(&cfg.peer_cookie[0], nla_data(info->attrs[L2TP_ATTR_PEER_COOKIE]), len);
  356. }
  357. if (info->attrs[L2TP_ATTR_IFNAME])
  358. cfg.ifname = nla_data(info->attrs[L2TP_ATTR_IFNAME]);
  359. if (info->attrs[L2TP_ATTR_VLAN_ID])
  360. cfg.vlan_id = nla_get_u16(info->attrs[L2TP_ATTR_VLAN_ID]);
  361. }
  362. if (info->attrs[L2TP_ATTR_DEBUG])
  363. cfg.debug = nla_get_u32(info->attrs[L2TP_ATTR_DEBUG]);
  364. if (info->attrs[L2TP_ATTR_RECV_SEQ])
  365. cfg.recv_seq = nla_get_u8(info->attrs[L2TP_ATTR_RECV_SEQ]);
  366. if (info->attrs[L2TP_ATTR_SEND_SEQ])
  367. cfg.send_seq = nla_get_u8(info->attrs[L2TP_ATTR_SEND_SEQ]);
  368. if (info->attrs[L2TP_ATTR_LNS_MODE])
  369. cfg.lns_mode = nla_get_u8(info->attrs[L2TP_ATTR_LNS_MODE]);
  370. if (info->attrs[L2TP_ATTR_RECV_TIMEOUT])
  371. cfg.reorder_timeout = nla_get_msecs(info->attrs[L2TP_ATTR_RECV_TIMEOUT]);
  372. if (info->attrs[L2TP_ATTR_MTU])
  373. cfg.mtu = nla_get_u16(info->attrs[L2TP_ATTR_MTU]);
  374. if (info->attrs[L2TP_ATTR_MRU])
  375. cfg.mru = nla_get_u16(info->attrs[L2TP_ATTR_MRU]);
  376. if ((l2tp_nl_cmd_ops[cfg.pw_type] == NULL) ||
  377. (l2tp_nl_cmd_ops[cfg.pw_type]->session_create == NULL)) {
  378. ret = -EPROTONOSUPPORT;
  379. goto out;
  380. }
  381. /* Check that pseudowire-specific params are present */
  382. switch (cfg.pw_type) {
  383. case L2TP_PWTYPE_NONE:
  384. break;
  385. case L2TP_PWTYPE_ETH_VLAN:
  386. if (!info->attrs[L2TP_ATTR_VLAN_ID]) {
  387. ret = -EINVAL;
  388. goto out;
  389. }
  390. break;
  391. case L2TP_PWTYPE_ETH:
  392. break;
  393. case L2TP_PWTYPE_PPP:
  394. case L2TP_PWTYPE_PPP_AC:
  395. break;
  396. case L2TP_PWTYPE_IP:
  397. default:
  398. ret = -EPROTONOSUPPORT;
  399. break;
  400. }
  401. ret = l2tp_nl_cmd_ops[cfg.pw_type]->session_create(net, tunnel,
  402. session_id,
  403. peer_session_id,
  404. &cfg);
  405. out:
  406. return ret;
  407. }
  408. static int l2tp_nl_cmd_session_delete(struct sk_buff *skb, struct genl_info *info)
  409. {
  410. int ret = 0;
  411. struct l2tp_session *session;
  412. u16 pw_type;
  413. session = l2tp_nl_session_find(info);
  414. if (session == NULL) {
  415. ret = -ENODEV;
  416. goto out;
  417. }
  418. pw_type = session->pwtype;
  419. if (pw_type < __L2TP_PWTYPE_MAX)
  420. if (l2tp_nl_cmd_ops[pw_type] && l2tp_nl_cmd_ops[pw_type]->session_delete)
  421. ret = (*l2tp_nl_cmd_ops[pw_type]->session_delete)(session);
  422. out:
  423. return ret;
  424. }
  425. static int l2tp_nl_cmd_session_modify(struct sk_buff *skb, struct genl_info *info)
  426. {
  427. int ret = 0;
  428. struct l2tp_session *session;
  429. session = l2tp_nl_session_find(info);
  430. if (session == NULL) {
  431. ret = -ENODEV;
  432. goto out;
  433. }
  434. if (info->attrs[L2TP_ATTR_DEBUG])
  435. session->debug = nla_get_u32(info->attrs[L2TP_ATTR_DEBUG]);
  436. if (info->attrs[L2TP_ATTR_DATA_SEQ])
  437. session->data_seq = nla_get_u8(info->attrs[L2TP_ATTR_DATA_SEQ]);
  438. if (info->attrs[L2TP_ATTR_RECV_SEQ])
  439. session->recv_seq = nla_get_u8(info->attrs[L2TP_ATTR_RECV_SEQ]);
  440. if (info->attrs[L2TP_ATTR_SEND_SEQ])
  441. session->send_seq = nla_get_u8(info->attrs[L2TP_ATTR_SEND_SEQ]);
  442. if (info->attrs[L2TP_ATTR_LNS_MODE])
  443. session->lns_mode = nla_get_u8(info->attrs[L2TP_ATTR_LNS_MODE]);
  444. if (info->attrs[L2TP_ATTR_RECV_TIMEOUT])
  445. session->reorder_timeout = nla_get_msecs(info->attrs[L2TP_ATTR_RECV_TIMEOUT]);
  446. if (info->attrs[L2TP_ATTR_MTU])
  447. session->mtu = nla_get_u16(info->attrs[L2TP_ATTR_MTU]);
  448. if (info->attrs[L2TP_ATTR_MRU])
  449. session->mru = nla_get_u16(info->attrs[L2TP_ATTR_MRU]);
  450. out:
  451. return ret;
  452. }
  453. static int l2tp_nl_session_send(struct sk_buff *skb, u32 pid, u32 seq, int flags,
  454. struct l2tp_session *session)
  455. {
  456. void *hdr;
  457. struct nlattr *nest;
  458. struct l2tp_tunnel *tunnel = session->tunnel;
  459. struct sock *sk = NULL;
  460. sk = tunnel->sock;
  461. hdr = genlmsg_put(skb, pid, seq, &l2tp_nl_family, flags, L2TP_CMD_SESSION_GET);
  462. if (IS_ERR(hdr))
  463. return PTR_ERR(hdr);
  464. NLA_PUT_U32(skb, L2TP_ATTR_CONN_ID, tunnel->tunnel_id);
  465. NLA_PUT_U32(skb, L2TP_ATTR_SESSION_ID, session->session_id);
  466. NLA_PUT_U32(skb, L2TP_ATTR_PEER_CONN_ID, tunnel->peer_tunnel_id);
  467. NLA_PUT_U32(skb, L2TP_ATTR_PEER_SESSION_ID, session->peer_session_id);
  468. NLA_PUT_U32(skb, L2TP_ATTR_DEBUG, session->debug);
  469. NLA_PUT_U16(skb, L2TP_ATTR_PW_TYPE, session->pwtype);
  470. NLA_PUT_U16(skb, L2TP_ATTR_MTU, session->mtu);
  471. if (session->mru)
  472. NLA_PUT_U16(skb, L2TP_ATTR_MRU, session->mru);
  473. if (session->ifname && session->ifname[0])
  474. NLA_PUT_STRING(skb, L2TP_ATTR_IFNAME, session->ifname);
  475. if (session->cookie_len)
  476. NLA_PUT(skb, L2TP_ATTR_COOKIE, session->cookie_len, &session->cookie[0]);
  477. if (session->peer_cookie_len)
  478. NLA_PUT(skb, L2TP_ATTR_PEER_COOKIE, session->peer_cookie_len, &session->peer_cookie[0]);
  479. NLA_PUT_U8(skb, L2TP_ATTR_RECV_SEQ, session->recv_seq);
  480. NLA_PUT_U8(skb, L2TP_ATTR_SEND_SEQ, session->send_seq);
  481. NLA_PUT_U8(skb, L2TP_ATTR_LNS_MODE, session->lns_mode);
  482. #ifdef CONFIG_XFRM
  483. if ((sk) && (sk->sk_policy[0] || sk->sk_policy[1]))
  484. NLA_PUT_U8(skb, L2TP_ATTR_USING_IPSEC, 1);
  485. #endif
  486. if (session->reorder_timeout)
  487. NLA_PUT_MSECS(skb, L2TP_ATTR_RECV_TIMEOUT, session->reorder_timeout);
  488. nest = nla_nest_start(skb, L2TP_ATTR_STATS);
  489. if (nest == NULL)
  490. goto nla_put_failure;
  491. NLA_PUT_U64(skb, L2TP_ATTR_TX_PACKETS, session->stats.tx_packets);
  492. NLA_PUT_U64(skb, L2TP_ATTR_TX_BYTES, session->stats.tx_bytes);
  493. NLA_PUT_U64(skb, L2TP_ATTR_TX_ERRORS, session->stats.tx_errors);
  494. NLA_PUT_U64(skb, L2TP_ATTR_RX_PACKETS, session->stats.rx_packets);
  495. NLA_PUT_U64(skb, L2TP_ATTR_RX_BYTES, session->stats.rx_bytes);
  496. NLA_PUT_U64(skb, L2TP_ATTR_RX_SEQ_DISCARDS, session->stats.rx_seq_discards);
  497. NLA_PUT_U64(skb, L2TP_ATTR_RX_OOS_PACKETS, session->stats.rx_oos_packets);
  498. NLA_PUT_U64(skb, L2TP_ATTR_RX_ERRORS, session->stats.rx_errors);
  499. nla_nest_end(skb, nest);
  500. return genlmsg_end(skb, hdr);
  501. nla_put_failure:
  502. genlmsg_cancel(skb, hdr);
  503. return -1;
  504. }
  505. static int l2tp_nl_cmd_session_get(struct sk_buff *skb, struct genl_info *info)
  506. {
  507. struct l2tp_session *session;
  508. struct sk_buff *msg;
  509. int ret;
  510. session = l2tp_nl_session_find(info);
  511. if (session == NULL) {
  512. ret = -ENODEV;
  513. goto out;
  514. }
  515. msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  516. if (!msg) {
  517. ret = -ENOMEM;
  518. goto out;
  519. }
  520. ret = l2tp_nl_session_send(msg, info->snd_pid, info->snd_seq,
  521. 0, session);
  522. if (ret < 0)
  523. goto err_out;
  524. return genlmsg_unicast(genl_info_net(info), msg, info->snd_pid);
  525. err_out:
  526. nlmsg_free(msg);
  527. out:
  528. return ret;
  529. }
  530. static int l2tp_nl_cmd_session_dump(struct sk_buff *skb, struct netlink_callback *cb)
  531. {
  532. struct net *net = sock_net(skb->sk);
  533. struct l2tp_session *session;
  534. struct l2tp_tunnel *tunnel = NULL;
  535. int ti = cb->args[0];
  536. int si = cb->args[1];
  537. for (;;) {
  538. if (tunnel == NULL) {
  539. tunnel = l2tp_tunnel_find_nth(net, ti);
  540. if (tunnel == NULL)
  541. goto out;
  542. }
  543. session = l2tp_session_find_nth(tunnel, si);
  544. if (session == NULL) {
  545. ti++;
  546. tunnel = NULL;
  547. si = 0;
  548. continue;
  549. }
  550. if (l2tp_nl_session_send(skb, NETLINK_CB(cb->skb).pid,
  551. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  552. session) <= 0)
  553. break;
  554. si++;
  555. }
  556. out:
  557. cb->args[0] = ti;
  558. cb->args[1] = si;
  559. return skb->len;
  560. }
  561. static struct nla_policy l2tp_nl_policy[L2TP_ATTR_MAX + 1] = {
  562. [L2TP_ATTR_NONE] = { .type = NLA_UNSPEC, },
  563. [L2TP_ATTR_PW_TYPE] = { .type = NLA_U16, },
  564. [L2TP_ATTR_ENCAP_TYPE] = { .type = NLA_U16, },
  565. [L2TP_ATTR_OFFSET] = { .type = NLA_U16, },
  566. [L2TP_ATTR_DATA_SEQ] = { .type = NLA_U8, },
  567. [L2TP_ATTR_L2SPEC_TYPE] = { .type = NLA_U8, },
  568. [L2TP_ATTR_L2SPEC_LEN] = { .type = NLA_U8, },
  569. [L2TP_ATTR_PROTO_VERSION] = { .type = NLA_U8, },
  570. [L2TP_ATTR_CONN_ID] = { .type = NLA_U32, },
  571. [L2TP_ATTR_PEER_CONN_ID] = { .type = NLA_U32, },
  572. [L2TP_ATTR_SESSION_ID] = { .type = NLA_U32, },
  573. [L2TP_ATTR_PEER_SESSION_ID] = { .type = NLA_U32, },
  574. [L2TP_ATTR_UDP_CSUM] = { .type = NLA_U8, },
  575. [L2TP_ATTR_VLAN_ID] = { .type = NLA_U16, },
  576. [L2TP_ATTR_DEBUG] = { .type = NLA_U32, },
  577. [L2TP_ATTR_RECV_SEQ] = { .type = NLA_U8, },
  578. [L2TP_ATTR_SEND_SEQ] = { .type = NLA_U8, },
  579. [L2TP_ATTR_LNS_MODE] = { .type = NLA_U8, },
  580. [L2TP_ATTR_USING_IPSEC] = { .type = NLA_U8, },
  581. [L2TP_ATTR_RECV_TIMEOUT] = { .type = NLA_MSECS, },
  582. [L2TP_ATTR_FD] = { .type = NLA_U32, },
  583. [L2TP_ATTR_IP_SADDR] = { .type = NLA_U32, },
  584. [L2TP_ATTR_IP_DADDR] = { .type = NLA_U32, },
  585. [L2TP_ATTR_UDP_SPORT] = { .type = NLA_U16, },
  586. [L2TP_ATTR_UDP_DPORT] = { .type = NLA_U16, },
  587. [L2TP_ATTR_MTU] = { .type = NLA_U16, },
  588. [L2TP_ATTR_MRU] = { .type = NLA_U16, },
  589. [L2TP_ATTR_STATS] = { .type = NLA_NESTED, },
  590. [L2TP_ATTR_IFNAME] = {
  591. .type = NLA_NUL_STRING,
  592. .len = IFNAMSIZ - 1,
  593. },
  594. [L2TP_ATTR_COOKIE] = {
  595. .type = NLA_BINARY,
  596. .len = 8,
  597. },
  598. [L2TP_ATTR_PEER_COOKIE] = {
  599. .type = NLA_BINARY,
  600. .len = 8,
  601. },
  602. };
  603. static struct genl_ops l2tp_nl_ops[] = {
  604. {
  605. .cmd = L2TP_CMD_NOOP,
  606. .doit = l2tp_nl_cmd_noop,
  607. .policy = l2tp_nl_policy,
  608. /* can be retrieved by unprivileged users */
  609. },
  610. {
  611. .cmd = L2TP_CMD_TUNNEL_CREATE,
  612. .doit = l2tp_nl_cmd_tunnel_create,
  613. .policy = l2tp_nl_policy,
  614. .flags = GENL_ADMIN_PERM,
  615. },
  616. {
  617. .cmd = L2TP_CMD_TUNNEL_DELETE,
  618. .doit = l2tp_nl_cmd_tunnel_delete,
  619. .policy = l2tp_nl_policy,
  620. .flags = GENL_ADMIN_PERM,
  621. },
  622. {
  623. .cmd = L2TP_CMD_TUNNEL_MODIFY,
  624. .doit = l2tp_nl_cmd_tunnel_modify,
  625. .policy = l2tp_nl_policy,
  626. .flags = GENL_ADMIN_PERM,
  627. },
  628. {
  629. .cmd = L2TP_CMD_TUNNEL_GET,
  630. .doit = l2tp_nl_cmd_tunnel_get,
  631. .dumpit = l2tp_nl_cmd_tunnel_dump,
  632. .policy = l2tp_nl_policy,
  633. .flags = GENL_ADMIN_PERM,
  634. },
  635. {
  636. .cmd = L2TP_CMD_SESSION_CREATE,
  637. .doit = l2tp_nl_cmd_session_create,
  638. .policy = l2tp_nl_policy,
  639. .flags = GENL_ADMIN_PERM,
  640. },
  641. {
  642. .cmd = L2TP_CMD_SESSION_DELETE,
  643. .doit = l2tp_nl_cmd_session_delete,
  644. .policy = l2tp_nl_policy,
  645. .flags = GENL_ADMIN_PERM,
  646. },
  647. {
  648. .cmd = L2TP_CMD_SESSION_MODIFY,
  649. .doit = l2tp_nl_cmd_session_modify,
  650. .policy = l2tp_nl_policy,
  651. .flags = GENL_ADMIN_PERM,
  652. },
  653. {
  654. .cmd = L2TP_CMD_SESSION_GET,
  655. .doit = l2tp_nl_cmd_session_get,
  656. .dumpit = l2tp_nl_cmd_session_dump,
  657. .policy = l2tp_nl_policy,
  658. .flags = GENL_ADMIN_PERM,
  659. },
  660. };
  661. int l2tp_nl_register_ops(enum l2tp_pwtype pw_type, const struct l2tp_nl_cmd_ops *ops)
  662. {
  663. int ret;
  664. ret = -EINVAL;
  665. if (pw_type >= __L2TP_PWTYPE_MAX)
  666. goto err;
  667. genl_lock();
  668. ret = -EBUSY;
  669. if (l2tp_nl_cmd_ops[pw_type])
  670. goto out;
  671. l2tp_nl_cmd_ops[pw_type] = ops;
  672. ret = 0;
  673. out:
  674. genl_unlock();
  675. err:
  676. return ret;
  677. }
  678. EXPORT_SYMBOL_GPL(l2tp_nl_register_ops);
  679. void l2tp_nl_unregister_ops(enum l2tp_pwtype pw_type)
  680. {
  681. if (pw_type < __L2TP_PWTYPE_MAX) {
  682. genl_lock();
  683. l2tp_nl_cmd_ops[pw_type] = NULL;
  684. genl_unlock();
  685. }
  686. }
  687. EXPORT_SYMBOL_GPL(l2tp_nl_unregister_ops);
  688. static int l2tp_nl_init(void)
  689. {
  690. int err;
  691. printk(KERN_INFO "L2TP netlink interface\n");
  692. err = genl_register_family_with_ops(&l2tp_nl_family, l2tp_nl_ops,
  693. ARRAY_SIZE(l2tp_nl_ops));
  694. return err;
  695. }
  696. static void l2tp_nl_cleanup(void)
  697. {
  698. genl_unregister_family(&l2tp_nl_family);
  699. }
  700. module_init(l2tp_nl_init);
  701. module_exit(l2tp_nl_cleanup);
  702. MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
  703. MODULE_DESCRIPTION("L2TP netlink");
  704. MODULE_LICENSE("GPL");
  705. MODULE_VERSION("1.0");
  706. MODULE_ALIAS("net-pf-" __stringify(PF_NETLINK) "-proto-" \
  707. __stringify(NETLINK_GENERIC) "-type-" "l2tp");