l2tp_netlink.c 23 KB

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