l2tp_netlink.c 26 KB

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