l2tp_netlink.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  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. if (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. goto nla_put_failure;
  201. nest = nla_nest_start(skb, L2TP_ATTR_STATS);
  202. if (nest == NULL)
  203. goto nla_put_failure;
  204. if (nla_put_u64(skb, L2TP_ATTR_TX_PACKETS, tunnel->stats.tx_packets) ||
  205. nla_put_u64(skb, L2TP_ATTR_TX_BYTES, tunnel->stats.tx_bytes) ||
  206. nla_put_u64(skb, L2TP_ATTR_TX_ERRORS, tunnel->stats.tx_errors) ||
  207. nla_put_u64(skb, L2TP_ATTR_RX_PACKETS, tunnel->stats.rx_packets) ||
  208. nla_put_u64(skb, L2TP_ATTR_RX_BYTES, tunnel->stats.rx_bytes) ||
  209. nla_put_u64(skb, L2TP_ATTR_RX_SEQ_DISCARDS,
  210. tunnel->stats.rx_seq_discards) ||
  211. nla_put_u64(skb, L2TP_ATTR_RX_OOS_PACKETS,
  212. tunnel->stats.rx_oos_packets) ||
  213. nla_put_u64(skb, L2TP_ATTR_RX_ERRORS, tunnel->stats.rx_errors))
  214. goto nla_put_failure;
  215. nla_nest_end(skb, nest);
  216. sk = tunnel->sock;
  217. if (!sk)
  218. goto out;
  219. inet = inet_sk(sk);
  220. switch (tunnel->encap) {
  221. case L2TP_ENCAPTYPE_UDP:
  222. if (nla_put_u16(skb, L2TP_ATTR_UDP_SPORT, ntohs(inet->inet_sport)) ||
  223. nla_put_u16(skb, L2TP_ATTR_UDP_DPORT, ntohs(inet->inet_dport)) ||
  224. nla_put_u8(skb, L2TP_ATTR_UDP_CSUM,
  225. (sk->sk_no_check != UDP_CSUM_NOXMIT)))
  226. goto nla_put_failure;
  227. /* NOBREAK */
  228. case L2TP_ENCAPTYPE_IP:
  229. if (nla_put_be32(skb, L2TP_ATTR_IP_SADDR, inet->inet_saddr) ||
  230. nla_put_be32(skb, L2TP_ATTR_IP_DADDR, inet->inet_daddr))
  231. goto nla_put_failure;
  232. break;
  233. }
  234. out:
  235. return genlmsg_end(skb, hdr);
  236. nla_put_failure:
  237. genlmsg_cancel(skb, hdr);
  238. return -1;
  239. }
  240. static int l2tp_nl_cmd_tunnel_get(struct sk_buff *skb, struct genl_info *info)
  241. {
  242. struct l2tp_tunnel *tunnel;
  243. struct sk_buff *msg;
  244. u32 tunnel_id;
  245. int ret = -ENOBUFS;
  246. struct net *net = genl_info_net(info);
  247. if (!info->attrs[L2TP_ATTR_CONN_ID]) {
  248. ret = -EINVAL;
  249. goto out;
  250. }
  251. tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
  252. tunnel = l2tp_tunnel_find(net, tunnel_id);
  253. if (tunnel == NULL) {
  254. ret = -ENODEV;
  255. goto out;
  256. }
  257. msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  258. if (!msg) {
  259. ret = -ENOMEM;
  260. goto out;
  261. }
  262. ret = l2tp_nl_tunnel_send(msg, info->snd_pid, info->snd_seq,
  263. NLM_F_ACK, tunnel);
  264. if (ret < 0)
  265. goto err_out;
  266. return genlmsg_unicast(net, msg, info->snd_pid);
  267. err_out:
  268. nlmsg_free(msg);
  269. out:
  270. return ret;
  271. }
  272. static int l2tp_nl_cmd_tunnel_dump(struct sk_buff *skb, struct netlink_callback *cb)
  273. {
  274. int ti = cb->args[0];
  275. struct l2tp_tunnel *tunnel;
  276. struct net *net = sock_net(skb->sk);
  277. for (;;) {
  278. tunnel = l2tp_tunnel_find_nth(net, ti);
  279. if (tunnel == NULL)
  280. goto out;
  281. if (l2tp_nl_tunnel_send(skb, NETLINK_CB(cb->skb).pid,
  282. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  283. tunnel) <= 0)
  284. goto out;
  285. ti++;
  286. }
  287. out:
  288. cb->args[0] = ti;
  289. return skb->len;
  290. }
  291. static int l2tp_nl_cmd_session_create(struct sk_buff *skb, struct genl_info *info)
  292. {
  293. u32 tunnel_id = 0;
  294. u32 session_id;
  295. u32 peer_session_id;
  296. int ret = 0;
  297. struct l2tp_tunnel *tunnel;
  298. struct l2tp_session *session;
  299. struct l2tp_session_cfg cfg = { 0, };
  300. struct net *net = genl_info_net(info);
  301. if (!info->attrs[L2TP_ATTR_CONN_ID]) {
  302. ret = -EINVAL;
  303. goto out;
  304. }
  305. tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
  306. tunnel = l2tp_tunnel_find(net, tunnel_id);
  307. if (!tunnel) {
  308. ret = -ENODEV;
  309. goto out;
  310. }
  311. if (!info->attrs[L2TP_ATTR_SESSION_ID]) {
  312. ret = -EINVAL;
  313. goto out;
  314. }
  315. session_id = nla_get_u32(info->attrs[L2TP_ATTR_SESSION_ID]);
  316. session = l2tp_session_find(net, tunnel, session_id);
  317. if (session) {
  318. ret = -EEXIST;
  319. goto out;
  320. }
  321. if (!info->attrs[L2TP_ATTR_PEER_SESSION_ID]) {
  322. ret = -EINVAL;
  323. goto out;
  324. }
  325. peer_session_id = nla_get_u32(info->attrs[L2TP_ATTR_PEER_SESSION_ID]);
  326. if (!info->attrs[L2TP_ATTR_PW_TYPE]) {
  327. ret = -EINVAL;
  328. goto out;
  329. }
  330. cfg.pw_type = nla_get_u16(info->attrs[L2TP_ATTR_PW_TYPE]);
  331. if (cfg.pw_type >= __L2TP_PWTYPE_MAX) {
  332. ret = -EINVAL;
  333. goto out;
  334. }
  335. if (tunnel->version > 2) {
  336. if (info->attrs[L2TP_ATTR_OFFSET])
  337. cfg.offset = nla_get_u16(info->attrs[L2TP_ATTR_OFFSET]);
  338. if (info->attrs[L2TP_ATTR_DATA_SEQ])
  339. cfg.data_seq = nla_get_u8(info->attrs[L2TP_ATTR_DATA_SEQ]);
  340. cfg.l2specific_type = L2TP_L2SPECTYPE_DEFAULT;
  341. if (info->attrs[L2TP_ATTR_L2SPEC_TYPE])
  342. cfg.l2specific_type = nla_get_u8(info->attrs[L2TP_ATTR_L2SPEC_TYPE]);
  343. cfg.l2specific_len = 4;
  344. if (info->attrs[L2TP_ATTR_L2SPEC_LEN])
  345. cfg.l2specific_len = nla_get_u8(info->attrs[L2TP_ATTR_L2SPEC_LEN]);
  346. if (info->attrs[L2TP_ATTR_COOKIE]) {
  347. u16 len = nla_len(info->attrs[L2TP_ATTR_COOKIE]);
  348. if (len > 8) {
  349. ret = -EINVAL;
  350. goto out;
  351. }
  352. cfg.cookie_len = len;
  353. memcpy(&cfg.cookie[0], nla_data(info->attrs[L2TP_ATTR_COOKIE]), len);
  354. }
  355. if (info->attrs[L2TP_ATTR_PEER_COOKIE]) {
  356. u16 len = nla_len(info->attrs[L2TP_ATTR_PEER_COOKIE]);
  357. if (len > 8) {
  358. ret = -EINVAL;
  359. goto out;
  360. }
  361. cfg.peer_cookie_len = len;
  362. memcpy(&cfg.peer_cookie[0], nla_data(info->attrs[L2TP_ATTR_PEER_COOKIE]), len);
  363. }
  364. if (info->attrs[L2TP_ATTR_IFNAME])
  365. cfg.ifname = nla_data(info->attrs[L2TP_ATTR_IFNAME]);
  366. if (info->attrs[L2TP_ATTR_VLAN_ID])
  367. cfg.vlan_id = nla_get_u16(info->attrs[L2TP_ATTR_VLAN_ID]);
  368. }
  369. if (info->attrs[L2TP_ATTR_DEBUG])
  370. cfg.debug = nla_get_u32(info->attrs[L2TP_ATTR_DEBUG]);
  371. if (info->attrs[L2TP_ATTR_RECV_SEQ])
  372. cfg.recv_seq = nla_get_u8(info->attrs[L2TP_ATTR_RECV_SEQ]);
  373. if (info->attrs[L2TP_ATTR_SEND_SEQ])
  374. cfg.send_seq = nla_get_u8(info->attrs[L2TP_ATTR_SEND_SEQ]);
  375. if (info->attrs[L2TP_ATTR_LNS_MODE])
  376. cfg.lns_mode = nla_get_u8(info->attrs[L2TP_ATTR_LNS_MODE]);
  377. if (info->attrs[L2TP_ATTR_RECV_TIMEOUT])
  378. cfg.reorder_timeout = nla_get_msecs(info->attrs[L2TP_ATTR_RECV_TIMEOUT]);
  379. if (info->attrs[L2TP_ATTR_MTU])
  380. cfg.mtu = nla_get_u16(info->attrs[L2TP_ATTR_MTU]);
  381. if (info->attrs[L2TP_ATTR_MRU])
  382. cfg.mru = nla_get_u16(info->attrs[L2TP_ATTR_MRU]);
  383. if ((l2tp_nl_cmd_ops[cfg.pw_type] == NULL) ||
  384. (l2tp_nl_cmd_ops[cfg.pw_type]->session_create == NULL)) {
  385. ret = -EPROTONOSUPPORT;
  386. goto out;
  387. }
  388. /* Check that pseudowire-specific params are present */
  389. switch (cfg.pw_type) {
  390. case L2TP_PWTYPE_NONE:
  391. break;
  392. case L2TP_PWTYPE_ETH_VLAN:
  393. if (!info->attrs[L2TP_ATTR_VLAN_ID]) {
  394. ret = -EINVAL;
  395. goto out;
  396. }
  397. break;
  398. case L2TP_PWTYPE_ETH:
  399. break;
  400. case L2TP_PWTYPE_PPP:
  401. case L2TP_PWTYPE_PPP_AC:
  402. break;
  403. case L2TP_PWTYPE_IP:
  404. default:
  405. ret = -EPROTONOSUPPORT;
  406. break;
  407. }
  408. ret = -EPROTONOSUPPORT;
  409. if (l2tp_nl_cmd_ops[cfg.pw_type]->session_create)
  410. ret = (*l2tp_nl_cmd_ops[cfg.pw_type]->session_create)(net, tunnel_id,
  411. session_id, peer_session_id, &cfg);
  412. out:
  413. return ret;
  414. }
  415. static int l2tp_nl_cmd_session_delete(struct sk_buff *skb, struct genl_info *info)
  416. {
  417. int ret = 0;
  418. struct l2tp_session *session;
  419. u16 pw_type;
  420. session = l2tp_nl_session_find(info);
  421. if (session == NULL) {
  422. ret = -ENODEV;
  423. goto out;
  424. }
  425. pw_type = session->pwtype;
  426. if (pw_type < __L2TP_PWTYPE_MAX)
  427. if (l2tp_nl_cmd_ops[pw_type] && l2tp_nl_cmd_ops[pw_type]->session_delete)
  428. ret = (*l2tp_nl_cmd_ops[pw_type]->session_delete)(session);
  429. out:
  430. return ret;
  431. }
  432. static int l2tp_nl_cmd_session_modify(struct sk_buff *skb, struct genl_info *info)
  433. {
  434. int ret = 0;
  435. struct l2tp_session *session;
  436. session = l2tp_nl_session_find(info);
  437. if (session == NULL) {
  438. ret = -ENODEV;
  439. goto out;
  440. }
  441. if (info->attrs[L2TP_ATTR_DEBUG])
  442. session->debug = nla_get_u32(info->attrs[L2TP_ATTR_DEBUG]);
  443. if (info->attrs[L2TP_ATTR_DATA_SEQ])
  444. session->data_seq = nla_get_u8(info->attrs[L2TP_ATTR_DATA_SEQ]);
  445. if (info->attrs[L2TP_ATTR_RECV_SEQ])
  446. session->recv_seq = nla_get_u8(info->attrs[L2TP_ATTR_RECV_SEQ]);
  447. if (info->attrs[L2TP_ATTR_SEND_SEQ])
  448. session->send_seq = nla_get_u8(info->attrs[L2TP_ATTR_SEND_SEQ]);
  449. if (info->attrs[L2TP_ATTR_LNS_MODE])
  450. session->lns_mode = nla_get_u8(info->attrs[L2TP_ATTR_LNS_MODE]);
  451. if (info->attrs[L2TP_ATTR_RECV_TIMEOUT])
  452. session->reorder_timeout = nla_get_msecs(info->attrs[L2TP_ATTR_RECV_TIMEOUT]);
  453. if (info->attrs[L2TP_ATTR_MTU])
  454. session->mtu = nla_get_u16(info->attrs[L2TP_ATTR_MTU]);
  455. if (info->attrs[L2TP_ATTR_MRU])
  456. session->mru = nla_get_u16(info->attrs[L2TP_ATTR_MRU]);
  457. out:
  458. return ret;
  459. }
  460. static int l2tp_nl_session_send(struct sk_buff *skb, u32 pid, u32 seq, int flags,
  461. struct l2tp_session *session)
  462. {
  463. void *hdr;
  464. struct nlattr *nest;
  465. struct l2tp_tunnel *tunnel = session->tunnel;
  466. struct sock *sk = NULL;
  467. sk = tunnel->sock;
  468. hdr = genlmsg_put(skb, pid, seq, &l2tp_nl_family, flags, L2TP_CMD_SESSION_GET);
  469. if (IS_ERR(hdr))
  470. return PTR_ERR(hdr);
  471. if (nla_put_u32(skb, L2TP_ATTR_CONN_ID, tunnel->tunnel_id) ||
  472. nla_put_u32(skb, L2TP_ATTR_SESSION_ID, session->session_id) ||
  473. nla_put_u32(skb, L2TP_ATTR_PEER_CONN_ID, tunnel->peer_tunnel_id) ||
  474. nla_put_u32(skb, L2TP_ATTR_PEER_SESSION_ID,
  475. session->peer_session_id) ||
  476. nla_put_u32(skb, L2TP_ATTR_DEBUG, session->debug) ||
  477. nla_put_u16(skb, L2TP_ATTR_PW_TYPE, session->pwtype) ||
  478. nla_put_u16(skb, L2TP_ATTR_MTU, session->mtu) ||
  479. (session->mru &&
  480. nla_put_u16(skb, L2TP_ATTR_MRU, session->mru)))
  481. goto nla_put_failure;
  482. if ((session->ifname && session->ifname[0] &&
  483. nla_put_string(skb, L2TP_ATTR_IFNAME, session->ifname)) ||
  484. (session->cookie_len &&
  485. nla_put(skb, L2TP_ATTR_COOKIE, session->cookie_len,
  486. &session->cookie[0])) ||
  487. (session->peer_cookie_len &&
  488. nla_put(skb, L2TP_ATTR_PEER_COOKIE, session->peer_cookie_len,
  489. &session->peer_cookie[0])) ||
  490. nla_put_u8(skb, L2TP_ATTR_RECV_SEQ, session->recv_seq) ||
  491. nla_put_u8(skb, L2TP_ATTR_SEND_SEQ, session->send_seq) ||
  492. nla_put_u8(skb, L2TP_ATTR_LNS_MODE, session->lns_mode) ||
  493. #ifdef CONFIG_XFRM
  494. (((sk) && (sk->sk_policy[0] || sk->sk_policy[1])) &&
  495. nla_put_u8(skb, L2TP_ATTR_USING_IPSEC, 1)) ||
  496. #endif
  497. (session->reorder_timeout &&
  498. nla_put_msecs(skb, L2TP_ATTR_RECV_TIMEOUT, session->reorder_timeout)))
  499. goto nla_put_failure;
  500. nest = nla_nest_start(skb, L2TP_ATTR_STATS);
  501. if (nest == NULL)
  502. goto nla_put_failure;
  503. if (nla_put_u64(skb, L2TP_ATTR_TX_PACKETS, session->stats.tx_packets) ||
  504. nla_put_u64(skb, L2TP_ATTR_TX_BYTES, session->stats.tx_bytes) ||
  505. nla_put_u64(skb, L2TP_ATTR_TX_ERRORS, session->stats.tx_errors) ||
  506. nla_put_u64(skb, L2TP_ATTR_RX_PACKETS, session->stats.rx_packets) ||
  507. nla_put_u64(skb, L2TP_ATTR_RX_BYTES, session->stats.rx_bytes) ||
  508. nla_put_u64(skb, L2TP_ATTR_RX_SEQ_DISCARDS,
  509. session->stats.rx_seq_discards) ||
  510. nla_put_u64(skb, L2TP_ATTR_RX_OOS_PACKETS,
  511. session->stats.rx_oos_packets) ||
  512. nla_put_u64(skb, L2TP_ATTR_RX_ERRORS, session->stats.rx_errors))
  513. goto nla_put_failure;
  514. nla_nest_end(skb, nest);
  515. return genlmsg_end(skb, hdr);
  516. nla_put_failure:
  517. genlmsg_cancel(skb, hdr);
  518. return -1;
  519. }
  520. static int l2tp_nl_cmd_session_get(struct sk_buff *skb, struct genl_info *info)
  521. {
  522. struct l2tp_session *session;
  523. struct sk_buff *msg;
  524. int ret;
  525. session = l2tp_nl_session_find(info);
  526. if (session == NULL) {
  527. ret = -ENODEV;
  528. goto out;
  529. }
  530. msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  531. if (!msg) {
  532. ret = -ENOMEM;
  533. goto out;
  534. }
  535. ret = l2tp_nl_session_send(msg, info->snd_pid, info->snd_seq,
  536. 0, session);
  537. if (ret < 0)
  538. goto err_out;
  539. return genlmsg_unicast(genl_info_net(info), msg, info->snd_pid);
  540. err_out:
  541. nlmsg_free(msg);
  542. out:
  543. return ret;
  544. }
  545. static int l2tp_nl_cmd_session_dump(struct sk_buff *skb, struct netlink_callback *cb)
  546. {
  547. struct net *net = sock_net(skb->sk);
  548. struct l2tp_session *session;
  549. struct l2tp_tunnel *tunnel = NULL;
  550. int ti = cb->args[0];
  551. int si = cb->args[1];
  552. for (;;) {
  553. if (tunnel == NULL) {
  554. tunnel = l2tp_tunnel_find_nth(net, ti);
  555. if (tunnel == NULL)
  556. goto out;
  557. }
  558. session = l2tp_session_find_nth(tunnel, si);
  559. if (session == NULL) {
  560. ti++;
  561. tunnel = NULL;
  562. si = 0;
  563. continue;
  564. }
  565. if (l2tp_nl_session_send(skb, NETLINK_CB(cb->skb).pid,
  566. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  567. session) <= 0)
  568. break;
  569. si++;
  570. }
  571. out:
  572. cb->args[0] = ti;
  573. cb->args[1] = si;
  574. return skb->len;
  575. }
  576. static struct nla_policy l2tp_nl_policy[L2TP_ATTR_MAX + 1] = {
  577. [L2TP_ATTR_NONE] = { .type = NLA_UNSPEC, },
  578. [L2TP_ATTR_PW_TYPE] = { .type = NLA_U16, },
  579. [L2TP_ATTR_ENCAP_TYPE] = { .type = NLA_U16, },
  580. [L2TP_ATTR_OFFSET] = { .type = NLA_U16, },
  581. [L2TP_ATTR_DATA_SEQ] = { .type = NLA_U8, },
  582. [L2TP_ATTR_L2SPEC_TYPE] = { .type = NLA_U8, },
  583. [L2TP_ATTR_L2SPEC_LEN] = { .type = NLA_U8, },
  584. [L2TP_ATTR_PROTO_VERSION] = { .type = NLA_U8, },
  585. [L2TP_ATTR_CONN_ID] = { .type = NLA_U32, },
  586. [L2TP_ATTR_PEER_CONN_ID] = { .type = NLA_U32, },
  587. [L2TP_ATTR_SESSION_ID] = { .type = NLA_U32, },
  588. [L2TP_ATTR_PEER_SESSION_ID] = { .type = NLA_U32, },
  589. [L2TP_ATTR_UDP_CSUM] = { .type = NLA_U8, },
  590. [L2TP_ATTR_VLAN_ID] = { .type = NLA_U16, },
  591. [L2TP_ATTR_DEBUG] = { .type = NLA_U32, },
  592. [L2TP_ATTR_RECV_SEQ] = { .type = NLA_U8, },
  593. [L2TP_ATTR_SEND_SEQ] = { .type = NLA_U8, },
  594. [L2TP_ATTR_LNS_MODE] = { .type = NLA_U8, },
  595. [L2TP_ATTR_USING_IPSEC] = { .type = NLA_U8, },
  596. [L2TP_ATTR_RECV_TIMEOUT] = { .type = NLA_MSECS, },
  597. [L2TP_ATTR_FD] = { .type = NLA_U32, },
  598. [L2TP_ATTR_IP_SADDR] = { .type = NLA_U32, },
  599. [L2TP_ATTR_IP_DADDR] = { .type = NLA_U32, },
  600. [L2TP_ATTR_UDP_SPORT] = { .type = NLA_U16, },
  601. [L2TP_ATTR_UDP_DPORT] = { .type = NLA_U16, },
  602. [L2TP_ATTR_MTU] = { .type = NLA_U16, },
  603. [L2TP_ATTR_MRU] = { .type = NLA_U16, },
  604. [L2TP_ATTR_STATS] = { .type = NLA_NESTED, },
  605. [L2TP_ATTR_IFNAME] = {
  606. .type = NLA_NUL_STRING,
  607. .len = IFNAMSIZ - 1,
  608. },
  609. [L2TP_ATTR_COOKIE] = {
  610. .type = NLA_BINARY,
  611. .len = 8,
  612. },
  613. [L2TP_ATTR_PEER_COOKIE] = {
  614. .type = NLA_BINARY,
  615. .len = 8,
  616. },
  617. };
  618. static struct genl_ops l2tp_nl_ops[] = {
  619. {
  620. .cmd = L2TP_CMD_NOOP,
  621. .doit = l2tp_nl_cmd_noop,
  622. .policy = l2tp_nl_policy,
  623. /* can be retrieved by unprivileged users */
  624. },
  625. {
  626. .cmd = L2TP_CMD_TUNNEL_CREATE,
  627. .doit = l2tp_nl_cmd_tunnel_create,
  628. .policy = l2tp_nl_policy,
  629. .flags = GENL_ADMIN_PERM,
  630. },
  631. {
  632. .cmd = L2TP_CMD_TUNNEL_DELETE,
  633. .doit = l2tp_nl_cmd_tunnel_delete,
  634. .policy = l2tp_nl_policy,
  635. .flags = GENL_ADMIN_PERM,
  636. },
  637. {
  638. .cmd = L2TP_CMD_TUNNEL_MODIFY,
  639. .doit = l2tp_nl_cmd_tunnel_modify,
  640. .policy = l2tp_nl_policy,
  641. .flags = GENL_ADMIN_PERM,
  642. },
  643. {
  644. .cmd = L2TP_CMD_TUNNEL_GET,
  645. .doit = l2tp_nl_cmd_tunnel_get,
  646. .dumpit = l2tp_nl_cmd_tunnel_dump,
  647. .policy = l2tp_nl_policy,
  648. .flags = GENL_ADMIN_PERM,
  649. },
  650. {
  651. .cmd = L2TP_CMD_SESSION_CREATE,
  652. .doit = l2tp_nl_cmd_session_create,
  653. .policy = l2tp_nl_policy,
  654. .flags = GENL_ADMIN_PERM,
  655. },
  656. {
  657. .cmd = L2TP_CMD_SESSION_DELETE,
  658. .doit = l2tp_nl_cmd_session_delete,
  659. .policy = l2tp_nl_policy,
  660. .flags = GENL_ADMIN_PERM,
  661. },
  662. {
  663. .cmd = L2TP_CMD_SESSION_MODIFY,
  664. .doit = l2tp_nl_cmd_session_modify,
  665. .policy = l2tp_nl_policy,
  666. .flags = GENL_ADMIN_PERM,
  667. },
  668. {
  669. .cmd = L2TP_CMD_SESSION_GET,
  670. .doit = l2tp_nl_cmd_session_get,
  671. .dumpit = l2tp_nl_cmd_session_dump,
  672. .policy = l2tp_nl_policy,
  673. .flags = GENL_ADMIN_PERM,
  674. },
  675. };
  676. int l2tp_nl_register_ops(enum l2tp_pwtype pw_type, const struct l2tp_nl_cmd_ops *ops)
  677. {
  678. int ret;
  679. ret = -EINVAL;
  680. if (pw_type >= __L2TP_PWTYPE_MAX)
  681. goto err;
  682. genl_lock();
  683. ret = -EBUSY;
  684. if (l2tp_nl_cmd_ops[pw_type])
  685. goto out;
  686. l2tp_nl_cmd_ops[pw_type] = ops;
  687. ret = 0;
  688. out:
  689. genl_unlock();
  690. err:
  691. return ret;
  692. }
  693. EXPORT_SYMBOL_GPL(l2tp_nl_register_ops);
  694. void l2tp_nl_unregister_ops(enum l2tp_pwtype pw_type)
  695. {
  696. if (pw_type < __L2TP_PWTYPE_MAX) {
  697. genl_lock();
  698. l2tp_nl_cmd_ops[pw_type] = NULL;
  699. genl_unlock();
  700. }
  701. }
  702. EXPORT_SYMBOL_GPL(l2tp_nl_unregister_ops);
  703. static int l2tp_nl_init(void)
  704. {
  705. int err;
  706. printk(KERN_INFO "L2TP netlink interface\n");
  707. err = genl_register_family_with_ops(&l2tp_nl_family, l2tp_nl_ops,
  708. ARRAY_SIZE(l2tp_nl_ops));
  709. return err;
  710. }
  711. static void l2tp_nl_cleanup(void)
  712. {
  713. genl_unregister_family(&l2tp_nl_family);
  714. }
  715. module_init(l2tp_nl_init);
  716. module_exit(l2tp_nl_cleanup);
  717. MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
  718. MODULE_DESCRIPTION("L2TP netlink");
  719. MODULE_LICENSE("GPL");
  720. MODULE_VERSION("1.0");
  721. MODULE_ALIAS("net-pf-" __stringify(PF_NETLINK) "-proto-" \
  722. __stringify(NETLINK_GENERIC) "-type-" "l2tp");