l2tp_netlink.c 23 KB

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