l2tp_netlink.c 26 KB

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