l2tp_netlink.c 27 KB

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