l2tp_netlink.c 27 KB

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