l2tp_netlink.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  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. if ((l2tp_nl_cmd_ops[cfg.pw_type] == NULL) ||
  481. (l2tp_nl_cmd_ops[cfg.pw_type]->session_create == NULL)) {
  482. ret = -EPROTONOSUPPORT;
  483. goto out;
  484. }
  485. /* Check that pseudowire-specific params are present */
  486. switch (cfg.pw_type) {
  487. case L2TP_PWTYPE_NONE:
  488. break;
  489. case L2TP_PWTYPE_ETH_VLAN:
  490. if (!info->attrs[L2TP_ATTR_VLAN_ID]) {
  491. ret = -EINVAL;
  492. goto out;
  493. }
  494. break;
  495. case L2TP_PWTYPE_ETH:
  496. break;
  497. case L2TP_PWTYPE_PPP:
  498. case L2TP_PWTYPE_PPP_AC:
  499. break;
  500. case L2TP_PWTYPE_IP:
  501. default:
  502. ret = -EPROTONOSUPPORT;
  503. break;
  504. }
  505. ret = -EPROTONOSUPPORT;
  506. if (l2tp_nl_cmd_ops[cfg.pw_type]->session_create)
  507. ret = (*l2tp_nl_cmd_ops[cfg.pw_type]->session_create)(net, tunnel_id,
  508. session_id, peer_session_id, &cfg);
  509. if (ret >= 0) {
  510. session = l2tp_session_find(net, tunnel, session_id);
  511. if (session)
  512. ret = l2tp_session_notify(&l2tp_nl_family, info, session,
  513. L2TP_CMD_SESSION_CREATE);
  514. }
  515. out:
  516. return ret;
  517. }
  518. static int l2tp_nl_cmd_session_delete(struct sk_buff *skb, struct genl_info *info)
  519. {
  520. int ret = 0;
  521. struct l2tp_session *session;
  522. u16 pw_type;
  523. session = l2tp_nl_session_find(info);
  524. if (session == NULL) {
  525. ret = -ENODEV;
  526. goto out;
  527. }
  528. l2tp_session_notify(&l2tp_nl_family, info,
  529. session, L2TP_CMD_SESSION_DELETE);
  530. pw_type = session->pwtype;
  531. if (pw_type < __L2TP_PWTYPE_MAX)
  532. if (l2tp_nl_cmd_ops[pw_type] && l2tp_nl_cmd_ops[pw_type]->session_delete)
  533. ret = (*l2tp_nl_cmd_ops[pw_type]->session_delete)(session);
  534. out:
  535. return ret;
  536. }
  537. static int l2tp_nl_cmd_session_modify(struct sk_buff *skb, struct genl_info *info)
  538. {
  539. int ret = 0;
  540. struct l2tp_session *session;
  541. session = l2tp_nl_session_find(info);
  542. if (session == NULL) {
  543. ret = -ENODEV;
  544. goto out;
  545. }
  546. if (info->attrs[L2TP_ATTR_DEBUG])
  547. session->debug = nla_get_u32(info->attrs[L2TP_ATTR_DEBUG]);
  548. if (info->attrs[L2TP_ATTR_DATA_SEQ])
  549. session->data_seq = nla_get_u8(info->attrs[L2TP_ATTR_DATA_SEQ]);
  550. if (info->attrs[L2TP_ATTR_RECV_SEQ])
  551. session->recv_seq = nla_get_u8(info->attrs[L2TP_ATTR_RECV_SEQ]);
  552. if (info->attrs[L2TP_ATTR_SEND_SEQ]) {
  553. session->send_seq = nla_get_u8(info->attrs[L2TP_ATTR_SEND_SEQ]);
  554. l2tp_session_set_header_len(session, session->tunnel->version);
  555. }
  556. if (info->attrs[L2TP_ATTR_LNS_MODE])
  557. session->lns_mode = nla_get_u8(info->attrs[L2TP_ATTR_LNS_MODE]);
  558. if (info->attrs[L2TP_ATTR_RECV_TIMEOUT])
  559. session->reorder_timeout = nla_get_msecs(info->attrs[L2TP_ATTR_RECV_TIMEOUT]);
  560. if (info->attrs[L2TP_ATTR_MTU])
  561. session->mtu = nla_get_u16(info->attrs[L2TP_ATTR_MTU]);
  562. if (info->attrs[L2TP_ATTR_MRU])
  563. session->mru = nla_get_u16(info->attrs[L2TP_ATTR_MRU]);
  564. ret = l2tp_session_notify(&l2tp_nl_family, info,
  565. session, L2TP_CMD_SESSION_MODIFY);
  566. out:
  567. return ret;
  568. }
  569. static int l2tp_nl_session_send(struct sk_buff *skb, u32 portid, u32 seq, int flags,
  570. struct l2tp_session *session, u8 cmd)
  571. {
  572. void *hdr;
  573. struct nlattr *nest;
  574. struct l2tp_tunnel *tunnel = session->tunnel;
  575. struct sock *sk = NULL;
  576. sk = tunnel->sock;
  577. hdr = genlmsg_put(skb, portid, seq, &l2tp_nl_family, flags, cmd);
  578. if (!hdr)
  579. return -EMSGSIZE;
  580. if (nla_put_u32(skb, L2TP_ATTR_CONN_ID, tunnel->tunnel_id) ||
  581. nla_put_u32(skb, L2TP_ATTR_SESSION_ID, session->session_id) ||
  582. nla_put_u32(skb, L2TP_ATTR_PEER_CONN_ID, tunnel->peer_tunnel_id) ||
  583. nla_put_u32(skb, L2TP_ATTR_PEER_SESSION_ID,
  584. session->peer_session_id) ||
  585. nla_put_u32(skb, L2TP_ATTR_DEBUG, session->debug) ||
  586. nla_put_u16(skb, L2TP_ATTR_PW_TYPE, session->pwtype) ||
  587. nla_put_u16(skb, L2TP_ATTR_MTU, session->mtu) ||
  588. (session->mru &&
  589. nla_put_u16(skb, L2TP_ATTR_MRU, session->mru)))
  590. goto nla_put_failure;
  591. if ((session->ifname[0] &&
  592. nla_put_string(skb, L2TP_ATTR_IFNAME, session->ifname)) ||
  593. (session->cookie_len &&
  594. nla_put(skb, L2TP_ATTR_COOKIE, session->cookie_len,
  595. &session->cookie[0])) ||
  596. (session->peer_cookie_len &&
  597. nla_put(skb, L2TP_ATTR_PEER_COOKIE, session->peer_cookie_len,
  598. &session->peer_cookie[0])) ||
  599. nla_put_u8(skb, L2TP_ATTR_RECV_SEQ, session->recv_seq) ||
  600. nla_put_u8(skb, L2TP_ATTR_SEND_SEQ, session->send_seq) ||
  601. nla_put_u8(skb, L2TP_ATTR_LNS_MODE, session->lns_mode) ||
  602. #ifdef CONFIG_XFRM
  603. (((sk) && (sk->sk_policy[0] || sk->sk_policy[1])) &&
  604. nla_put_u8(skb, L2TP_ATTR_USING_IPSEC, 1)) ||
  605. #endif
  606. (session->reorder_timeout &&
  607. nla_put_msecs(skb, L2TP_ATTR_RECV_TIMEOUT, session->reorder_timeout)))
  608. goto nla_put_failure;
  609. nest = nla_nest_start(skb, L2TP_ATTR_STATS);
  610. if (nest == NULL)
  611. goto nla_put_failure;
  612. if (nla_put_u64(skb, L2TP_ATTR_TX_PACKETS,
  613. atomic_long_read(&session->stats.tx_packets)) ||
  614. nla_put_u64(skb, L2TP_ATTR_TX_BYTES,
  615. atomic_long_read(&session->stats.tx_bytes)) ||
  616. nla_put_u64(skb, L2TP_ATTR_TX_ERRORS,
  617. atomic_long_read(&session->stats.tx_errors)) ||
  618. nla_put_u64(skb, L2TP_ATTR_RX_PACKETS,
  619. atomic_long_read(&session->stats.rx_packets)) ||
  620. nla_put_u64(skb, L2TP_ATTR_RX_BYTES,
  621. atomic_long_read(&session->stats.rx_bytes)) ||
  622. nla_put_u64(skb, L2TP_ATTR_RX_SEQ_DISCARDS,
  623. atomic_long_read(&session->stats.rx_seq_discards)) ||
  624. nla_put_u64(skb, L2TP_ATTR_RX_OOS_PACKETS,
  625. atomic_long_read(&session->stats.rx_oos_packets)) ||
  626. nla_put_u64(skb, L2TP_ATTR_RX_ERRORS,
  627. atomic_long_read(&session->stats.rx_errors)))
  628. goto nla_put_failure;
  629. nla_nest_end(skb, nest);
  630. genlmsg_end(skb, hdr);
  631. return 0;
  632. nla_put_failure:
  633. genlmsg_cancel(skb, hdr);
  634. return -1;
  635. }
  636. static int l2tp_nl_cmd_session_get(struct sk_buff *skb, struct genl_info *info)
  637. {
  638. struct l2tp_session *session;
  639. struct sk_buff *msg;
  640. int ret;
  641. session = l2tp_nl_session_find(info);
  642. if (session == NULL) {
  643. ret = -ENODEV;
  644. goto out;
  645. }
  646. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  647. if (!msg) {
  648. ret = -ENOMEM;
  649. goto out;
  650. }
  651. ret = l2tp_nl_session_send(msg, info->snd_portid, info->snd_seq,
  652. 0, session, L2TP_CMD_SESSION_GET);
  653. if (ret < 0)
  654. goto err_out;
  655. return genlmsg_unicast(genl_info_net(info), msg, info->snd_portid);
  656. err_out:
  657. nlmsg_free(msg);
  658. out:
  659. return ret;
  660. }
  661. static int l2tp_nl_cmd_session_dump(struct sk_buff *skb, struct netlink_callback *cb)
  662. {
  663. struct net *net = sock_net(skb->sk);
  664. struct l2tp_session *session;
  665. struct l2tp_tunnel *tunnel = NULL;
  666. int ti = cb->args[0];
  667. int si = cb->args[1];
  668. for (;;) {
  669. if (tunnel == NULL) {
  670. tunnel = l2tp_tunnel_find_nth(net, ti);
  671. if (tunnel == NULL)
  672. goto out;
  673. }
  674. session = l2tp_session_find_nth(tunnel, si);
  675. if (session == NULL) {
  676. ti++;
  677. tunnel = NULL;
  678. si = 0;
  679. continue;
  680. }
  681. if (l2tp_nl_session_send(skb, NETLINK_CB(cb->skb).portid,
  682. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  683. session, L2TP_CMD_SESSION_GET) < 0)
  684. break;
  685. si++;
  686. }
  687. out:
  688. cb->args[0] = ti;
  689. cb->args[1] = si;
  690. return skb->len;
  691. }
  692. static struct nla_policy l2tp_nl_policy[L2TP_ATTR_MAX + 1] = {
  693. [L2TP_ATTR_NONE] = { .type = NLA_UNSPEC, },
  694. [L2TP_ATTR_PW_TYPE] = { .type = NLA_U16, },
  695. [L2TP_ATTR_ENCAP_TYPE] = { .type = NLA_U16, },
  696. [L2TP_ATTR_OFFSET] = { .type = NLA_U16, },
  697. [L2TP_ATTR_DATA_SEQ] = { .type = NLA_U8, },
  698. [L2TP_ATTR_L2SPEC_TYPE] = { .type = NLA_U8, },
  699. [L2TP_ATTR_L2SPEC_LEN] = { .type = NLA_U8, },
  700. [L2TP_ATTR_PROTO_VERSION] = { .type = NLA_U8, },
  701. [L2TP_ATTR_CONN_ID] = { .type = NLA_U32, },
  702. [L2TP_ATTR_PEER_CONN_ID] = { .type = NLA_U32, },
  703. [L2TP_ATTR_SESSION_ID] = { .type = NLA_U32, },
  704. [L2TP_ATTR_PEER_SESSION_ID] = { .type = NLA_U32, },
  705. [L2TP_ATTR_UDP_CSUM] = { .type = NLA_U8, },
  706. [L2TP_ATTR_VLAN_ID] = { .type = NLA_U16, },
  707. [L2TP_ATTR_DEBUG] = { .type = NLA_U32, },
  708. [L2TP_ATTR_RECV_SEQ] = { .type = NLA_U8, },
  709. [L2TP_ATTR_SEND_SEQ] = { .type = NLA_U8, },
  710. [L2TP_ATTR_LNS_MODE] = { .type = NLA_U8, },
  711. [L2TP_ATTR_USING_IPSEC] = { .type = NLA_U8, },
  712. [L2TP_ATTR_RECV_TIMEOUT] = { .type = NLA_MSECS, },
  713. [L2TP_ATTR_FD] = { .type = NLA_U32, },
  714. [L2TP_ATTR_IP_SADDR] = { .type = NLA_U32, },
  715. [L2TP_ATTR_IP_DADDR] = { .type = NLA_U32, },
  716. [L2TP_ATTR_UDP_SPORT] = { .type = NLA_U16, },
  717. [L2TP_ATTR_UDP_DPORT] = { .type = NLA_U16, },
  718. [L2TP_ATTR_MTU] = { .type = NLA_U16, },
  719. [L2TP_ATTR_MRU] = { .type = NLA_U16, },
  720. [L2TP_ATTR_STATS] = { .type = NLA_NESTED, },
  721. [L2TP_ATTR_IP6_SADDR] = {
  722. .type = NLA_BINARY,
  723. .len = sizeof(struct in6_addr),
  724. },
  725. [L2TP_ATTR_IP6_DADDR] = {
  726. .type = NLA_BINARY,
  727. .len = sizeof(struct in6_addr),
  728. },
  729. [L2TP_ATTR_IFNAME] = {
  730. .type = NLA_NUL_STRING,
  731. .len = IFNAMSIZ - 1,
  732. },
  733. [L2TP_ATTR_COOKIE] = {
  734. .type = NLA_BINARY,
  735. .len = 8,
  736. },
  737. [L2TP_ATTR_PEER_COOKIE] = {
  738. .type = NLA_BINARY,
  739. .len = 8,
  740. },
  741. };
  742. static const struct genl_ops l2tp_nl_ops[] = {
  743. {
  744. .cmd = L2TP_CMD_NOOP,
  745. .doit = l2tp_nl_cmd_noop,
  746. .policy = l2tp_nl_policy,
  747. /* can be retrieved by unprivileged users */
  748. },
  749. {
  750. .cmd = L2TP_CMD_TUNNEL_CREATE,
  751. .doit = l2tp_nl_cmd_tunnel_create,
  752. .policy = l2tp_nl_policy,
  753. .flags = GENL_ADMIN_PERM,
  754. },
  755. {
  756. .cmd = L2TP_CMD_TUNNEL_DELETE,
  757. .doit = l2tp_nl_cmd_tunnel_delete,
  758. .policy = l2tp_nl_policy,
  759. .flags = GENL_ADMIN_PERM,
  760. },
  761. {
  762. .cmd = L2TP_CMD_TUNNEL_MODIFY,
  763. .doit = l2tp_nl_cmd_tunnel_modify,
  764. .policy = l2tp_nl_policy,
  765. .flags = GENL_ADMIN_PERM,
  766. },
  767. {
  768. .cmd = L2TP_CMD_TUNNEL_GET,
  769. .doit = l2tp_nl_cmd_tunnel_get,
  770. .dumpit = l2tp_nl_cmd_tunnel_dump,
  771. .policy = l2tp_nl_policy,
  772. .flags = GENL_ADMIN_PERM,
  773. },
  774. {
  775. .cmd = L2TP_CMD_SESSION_CREATE,
  776. .doit = l2tp_nl_cmd_session_create,
  777. .policy = l2tp_nl_policy,
  778. .flags = GENL_ADMIN_PERM,
  779. },
  780. {
  781. .cmd = L2TP_CMD_SESSION_DELETE,
  782. .doit = l2tp_nl_cmd_session_delete,
  783. .policy = l2tp_nl_policy,
  784. .flags = GENL_ADMIN_PERM,
  785. },
  786. {
  787. .cmd = L2TP_CMD_SESSION_MODIFY,
  788. .doit = l2tp_nl_cmd_session_modify,
  789. .policy = l2tp_nl_policy,
  790. .flags = GENL_ADMIN_PERM,
  791. },
  792. {
  793. .cmd = L2TP_CMD_SESSION_GET,
  794. .doit = l2tp_nl_cmd_session_get,
  795. .dumpit = l2tp_nl_cmd_session_dump,
  796. .policy = l2tp_nl_policy,
  797. .flags = GENL_ADMIN_PERM,
  798. },
  799. };
  800. int l2tp_nl_register_ops(enum l2tp_pwtype pw_type, const struct l2tp_nl_cmd_ops *ops)
  801. {
  802. int ret;
  803. ret = -EINVAL;
  804. if (pw_type >= __L2TP_PWTYPE_MAX)
  805. goto err;
  806. genl_lock();
  807. ret = -EBUSY;
  808. if (l2tp_nl_cmd_ops[pw_type])
  809. goto out;
  810. l2tp_nl_cmd_ops[pw_type] = ops;
  811. ret = 0;
  812. out:
  813. genl_unlock();
  814. err:
  815. return ret;
  816. }
  817. EXPORT_SYMBOL_GPL(l2tp_nl_register_ops);
  818. void l2tp_nl_unregister_ops(enum l2tp_pwtype pw_type)
  819. {
  820. if (pw_type < __L2TP_PWTYPE_MAX) {
  821. genl_lock();
  822. l2tp_nl_cmd_ops[pw_type] = NULL;
  823. genl_unlock();
  824. }
  825. }
  826. EXPORT_SYMBOL_GPL(l2tp_nl_unregister_ops);
  827. static int l2tp_nl_init(void)
  828. {
  829. pr_info("L2TP netlink interface\n");
  830. return genl_register_family_with_ops_groups(&l2tp_nl_family,
  831. l2tp_nl_ops,
  832. l2tp_multicast_group);
  833. }
  834. static void l2tp_nl_cleanup(void)
  835. {
  836. genl_unregister_family(&l2tp_nl_family);
  837. }
  838. module_init(l2tp_nl_init);
  839. module_exit(l2tp_nl_cleanup);
  840. MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
  841. MODULE_DESCRIPTION("L2TP netlink");
  842. MODULE_LICENSE("GPL");
  843. MODULE_VERSION("1.0");
  844. MODULE_ALIAS_GENL_FAMILY("l2tp");