l2tp_netlink.c 27 KB

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