l2tp_netlink.c 25 KB

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