l2tp_netlink.c 27 KB

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