l2tp_netlink.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927
  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. }
  142. if (info->attrs[L2TP_ATTR_DEBUG])
  143. cfg.debug = nla_get_u32(info->attrs[L2TP_ATTR_DEBUG]);
  144. tunnel = l2tp_tunnel_find(net, tunnel_id);
  145. if (tunnel != NULL) {
  146. ret = -EEXIST;
  147. goto out;
  148. }
  149. ret = -EINVAL;
  150. switch (cfg.encap) {
  151. case L2TP_ENCAPTYPE_UDP:
  152. case L2TP_ENCAPTYPE_IP:
  153. ret = l2tp_tunnel_create(net, fd, proto_version, tunnel_id,
  154. peer_tunnel_id, &cfg, &tunnel);
  155. break;
  156. }
  157. out:
  158. return ret;
  159. }
  160. static int l2tp_nl_cmd_tunnel_delete(struct sk_buff *skb, struct genl_info *info)
  161. {
  162. struct l2tp_tunnel *tunnel;
  163. u32 tunnel_id;
  164. int ret = 0;
  165. struct net *net = genl_info_net(info);
  166. if (!info->attrs[L2TP_ATTR_CONN_ID]) {
  167. ret = -EINVAL;
  168. goto out;
  169. }
  170. tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
  171. tunnel = l2tp_tunnel_find(net, tunnel_id);
  172. if (tunnel == NULL) {
  173. ret = -ENODEV;
  174. goto out;
  175. }
  176. (void) l2tp_tunnel_delete(tunnel);
  177. out:
  178. return ret;
  179. }
  180. static int l2tp_nl_cmd_tunnel_modify(struct sk_buff *skb, struct genl_info *info)
  181. {
  182. struct l2tp_tunnel *tunnel;
  183. u32 tunnel_id;
  184. int ret = 0;
  185. struct net *net = genl_info_net(info);
  186. if (!info->attrs[L2TP_ATTR_CONN_ID]) {
  187. ret = -EINVAL;
  188. goto out;
  189. }
  190. tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
  191. tunnel = l2tp_tunnel_find(net, tunnel_id);
  192. if (tunnel == NULL) {
  193. ret = -ENODEV;
  194. goto out;
  195. }
  196. if (info->attrs[L2TP_ATTR_DEBUG])
  197. tunnel->debug = nla_get_u32(info->attrs[L2TP_ATTR_DEBUG]);
  198. out:
  199. return ret;
  200. }
  201. static int l2tp_nl_tunnel_send(struct sk_buff *skb, u32 portid, u32 seq, int flags,
  202. struct l2tp_tunnel *tunnel)
  203. {
  204. void *hdr;
  205. struct nlattr *nest;
  206. struct sock *sk = NULL;
  207. struct inet_sock *inet;
  208. #if IS_ENABLED(CONFIG_IPV6)
  209. struct ipv6_pinfo *np = NULL;
  210. #endif
  211. struct l2tp_stats stats;
  212. unsigned int start;
  213. hdr = genlmsg_put(skb, portid, seq, &l2tp_nl_family, flags,
  214. L2TP_CMD_TUNNEL_GET);
  215. if (!hdr)
  216. return -EMSGSIZE;
  217. if (nla_put_u8(skb, L2TP_ATTR_PROTO_VERSION, tunnel->version) ||
  218. nla_put_u32(skb, L2TP_ATTR_CONN_ID, tunnel->tunnel_id) ||
  219. nla_put_u32(skb, L2TP_ATTR_PEER_CONN_ID, tunnel->peer_tunnel_id) ||
  220. nla_put_u32(skb, L2TP_ATTR_DEBUG, tunnel->debug) ||
  221. nla_put_u16(skb, L2TP_ATTR_ENCAP_TYPE, tunnel->encap))
  222. goto nla_put_failure;
  223. nest = nla_nest_start(skb, L2TP_ATTR_STATS);
  224. if (nest == NULL)
  225. goto nla_put_failure;
  226. do {
  227. start = u64_stats_fetch_begin(&tunnel->stats.syncp);
  228. stats.tx_packets = tunnel->stats.tx_packets;
  229. stats.tx_bytes = tunnel->stats.tx_bytes;
  230. stats.tx_errors = tunnel->stats.tx_errors;
  231. stats.rx_packets = tunnel->stats.rx_packets;
  232. stats.rx_bytes = tunnel->stats.rx_bytes;
  233. stats.rx_errors = tunnel->stats.rx_errors;
  234. stats.rx_seq_discards = tunnel->stats.rx_seq_discards;
  235. stats.rx_oos_packets = tunnel->stats.rx_oos_packets;
  236. } while (u64_stats_fetch_retry(&tunnel->stats.syncp, start));
  237. if (nla_put_u64(skb, L2TP_ATTR_TX_PACKETS, stats.tx_packets) ||
  238. nla_put_u64(skb, L2TP_ATTR_TX_BYTES, stats.tx_bytes) ||
  239. nla_put_u64(skb, L2TP_ATTR_TX_ERRORS, stats.tx_errors) ||
  240. nla_put_u64(skb, L2TP_ATTR_RX_PACKETS, stats.rx_packets) ||
  241. nla_put_u64(skb, L2TP_ATTR_RX_BYTES, stats.rx_bytes) ||
  242. nla_put_u64(skb, L2TP_ATTR_RX_SEQ_DISCARDS,
  243. stats.rx_seq_discards) ||
  244. nla_put_u64(skb, L2TP_ATTR_RX_OOS_PACKETS,
  245. stats.rx_oos_packets) ||
  246. nla_put_u64(skb, L2TP_ATTR_RX_ERRORS, stats.rx_errors))
  247. goto nla_put_failure;
  248. nla_nest_end(skb, nest);
  249. sk = tunnel->sock;
  250. if (!sk)
  251. goto out;
  252. #if IS_ENABLED(CONFIG_IPV6)
  253. if (sk->sk_family == AF_INET6)
  254. np = inet6_sk(sk);
  255. #endif
  256. inet = inet_sk(sk);
  257. switch (tunnel->encap) {
  258. case L2TP_ENCAPTYPE_UDP:
  259. if (nla_put_u16(skb, L2TP_ATTR_UDP_SPORT, ntohs(inet->inet_sport)) ||
  260. nla_put_u16(skb, L2TP_ATTR_UDP_DPORT, ntohs(inet->inet_dport)) ||
  261. nla_put_u8(skb, L2TP_ATTR_UDP_CSUM,
  262. (sk->sk_no_check != UDP_CSUM_NOXMIT)))
  263. goto nla_put_failure;
  264. /* NOBREAK */
  265. case L2TP_ENCAPTYPE_IP:
  266. #if IS_ENABLED(CONFIG_IPV6)
  267. if (np) {
  268. if (nla_put(skb, L2TP_ATTR_IP6_SADDR, sizeof(np->saddr),
  269. &np->saddr) ||
  270. nla_put(skb, L2TP_ATTR_IP6_DADDR, sizeof(np->daddr),
  271. &np->daddr))
  272. goto nla_put_failure;
  273. } else
  274. #endif
  275. if (nla_put_be32(skb, L2TP_ATTR_IP_SADDR, inet->inet_saddr) ||
  276. nla_put_be32(skb, L2TP_ATTR_IP_DADDR, inet->inet_daddr))
  277. goto nla_put_failure;
  278. break;
  279. }
  280. out:
  281. return genlmsg_end(skb, hdr);
  282. nla_put_failure:
  283. genlmsg_cancel(skb, hdr);
  284. return -1;
  285. }
  286. static int l2tp_nl_cmd_tunnel_get(struct sk_buff *skb, struct genl_info *info)
  287. {
  288. struct l2tp_tunnel *tunnel;
  289. struct sk_buff *msg;
  290. u32 tunnel_id;
  291. int ret = -ENOBUFS;
  292. struct net *net = genl_info_net(info);
  293. if (!info->attrs[L2TP_ATTR_CONN_ID]) {
  294. ret = -EINVAL;
  295. goto out;
  296. }
  297. tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
  298. tunnel = l2tp_tunnel_find(net, tunnel_id);
  299. if (tunnel == NULL) {
  300. ret = -ENODEV;
  301. goto out;
  302. }
  303. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  304. if (!msg) {
  305. ret = -ENOMEM;
  306. goto out;
  307. }
  308. ret = l2tp_nl_tunnel_send(msg, info->snd_portid, info->snd_seq,
  309. NLM_F_ACK, tunnel);
  310. if (ret < 0)
  311. goto err_out;
  312. return genlmsg_unicast(net, msg, info->snd_portid);
  313. err_out:
  314. nlmsg_free(msg);
  315. out:
  316. return ret;
  317. }
  318. static int l2tp_nl_cmd_tunnel_dump(struct sk_buff *skb, struct netlink_callback *cb)
  319. {
  320. int ti = cb->args[0];
  321. struct l2tp_tunnel *tunnel;
  322. struct net *net = sock_net(skb->sk);
  323. for (;;) {
  324. tunnel = l2tp_tunnel_find_nth(net, ti);
  325. if (tunnel == NULL)
  326. goto out;
  327. if (l2tp_nl_tunnel_send(skb, NETLINK_CB(cb->skb).portid,
  328. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  329. tunnel) <= 0)
  330. goto out;
  331. ti++;
  332. }
  333. out:
  334. cb->args[0] = ti;
  335. return skb->len;
  336. }
  337. static int l2tp_nl_cmd_session_create(struct sk_buff *skb, struct genl_info *info)
  338. {
  339. u32 tunnel_id = 0;
  340. u32 session_id;
  341. u32 peer_session_id;
  342. int ret = 0;
  343. struct l2tp_tunnel *tunnel;
  344. struct l2tp_session *session;
  345. struct l2tp_session_cfg cfg = { 0, };
  346. struct net *net = genl_info_net(info);
  347. if (!info->attrs[L2TP_ATTR_CONN_ID]) {
  348. ret = -EINVAL;
  349. goto out;
  350. }
  351. tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
  352. tunnel = l2tp_tunnel_find(net, tunnel_id);
  353. if (!tunnel) {
  354. ret = -ENODEV;
  355. goto out;
  356. }
  357. if (!info->attrs[L2TP_ATTR_SESSION_ID]) {
  358. ret = -EINVAL;
  359. goto out;
  360. }
  361. session_id = nla_get_u32(info->attrs[L2TP_ATTR_SESSION_ID]);
  362. session = l2tp_session_find(net, tunnel, session_id);
  363. if (session) {
  364. ret = -EEXIST;
  365. goto out;
  366. }
  367. if (!info->attrs[L2TP_ATTR_PEER_SESSION_ID]) {
  368. ret = -EINVAL;
  369. goto out;
  370. }
  371. peer_session_id = nla_get_u32(info->attrs[L2TP_ATTR_PEER_SESSION_ID]);
  372. if (!info->attrs[L2TP_ATTR_PW_TYPE]) {
  373. ret = -EINVAL;
  374. goto out;
  375. }
  376. cfg.pw_type = nla_get_u16(info->attrs[L2TP_ATTR_PW_TYPE]);
  377. if (cfg.pw_type >= __L2TP_PWTYPE_MAX) {
  378. ret = -EINVAL;
  379. goto out;
  380. }
  381. if (tunnel->version > 2) {
  382. if (info->attrs[L2TP_ATTR_OFFSET])
  383. cfg.offset = nla_get_u16(info->attrs[L2TP_ATTR_OFFSET]);
  384. if (info->attrs[L2TP_ATTR_DATA_SEQ])
  385. cfg.data_seq = nla_get_u8(info->attrs[L2TP_ATTR_DATA_SEQ]);
  386. cfg.l2specific_type = L2TP_L2SPECTYPE_DEFAULT;
  387. if (info->attrs[L2TP_ATTR_L2SPEC_TYPE])
  388. cfg.l2specific_type = nla_get_u8(info->attrs[L2TP_ATTR_L2SPEC_TYPE]);
  389. cfg.l2specific_len = 4;
  390. if (info->attrs[L2TP_ATTR_L2SPEC_LEN])
  391. cfg.l2specific_len = nla_get_u8(info->attrs[L2TP_ATTR_L2SPEC_LEN]);
  392. if (info->attrs[L2TP_ATTR_COOKIE]) {
  393. u16 len = nla_len(info->attrs[L2TP_ATTR_COOKIE]);
  394. if (len > 8) {
  395. ret = -EINVAL;
  396. goto out;
  397. }
  398. cfg.cookie_len = len;
  399. memcpy(&cfg.cookie[0], nla_data(info->attrs[L2TP_ATTR_COOKIE]), len);
  400. }
  401. if (info->attrs[L2TP_ATTR_PEER_COOKIE]) {
  402. u16 len = nla_len(info->attrs[L2TP_ATTR_PEER_COOKIE]);
  403. if (len > 8) {
  404. ret = -EINVAL;
  405. goto out;
  406. }
  407. cfg.peer_cookie_len = len;
  408. memcpy(&cfg.peer_cookie[0], nla_data(info->attrs[L2TP_ATTR_PEER_COOKIE]), len);
  409. }
  410. if (info->attrs[L2TP_ATTR_IFNAME])
  411. cfg.ifname = nla_data(info->attrs[L2TP_ATTR_IFNAME]);
  412. if (info->attrs[L2TP_ATTR_VLAN_ID])
  413. cfg.vlan_id = nla_get_u16(info->attrs[L2TP_ATTR_VLAN_ID]);
  414. }
  415. if (info->attrs[L2TP_ATTR_DEBUG])
  416. cfg.debug = nla_get_u32(info->attrs[L2TP_ATTR_DEBUG]);
  417. if (info->attrs[L2TP_ATTR_RECV_SEQ])
  418. cfg.recv_seq = nla_get_u8(info->attrs[L2TP_ATTR_RECV_SEQ]);
  419. if (info->attrs[L2TP_ATTR_SEND_SEQ])
  420. cfg.send_seq = nla_get_u8(info->attrs[L2TP_ATTR_SEND_SEQ]);
  421. if (info->attrs[L2TP_ATTR_LNS_MODE])
  422. cfg.lns_mode = nla_get_u8(info->attrs[L2TP_ATTR_LNS_MODE]);
  423. if (info->attrs[L2TP_ATTR_RECV_TIMEOUT])
  424. cfg.reorder_timeout = nla_get_msecs(info->attrs[L2TP_ATTR_RECV_TIMEOUT]);
  425. if (info->attrs[L2TP_ATTR_MTU])
  426. cfg.mtu = nla_get_u16(info->attrs[L2TP_ATTR_MTU]);
  427. if (info->attrs[L2TP_ATTR_MRU])
  428. cfg.mru = nla_get_u16(info->attrs[L2TP_ATTR_MRU]);
  429. if ((l2tp_nl_cmd_ops[cfg.pw_type] == NULL) ||
  430. (l2tp_nl_cmd_ops[cfg.pw_type]->session_create == NULL)) {
  431. ret = -EPROTONOSUPPORT;
  432. goto out;
  433. }
  434. /* Check that pseudowire-specific params are present */
  435. switch (cfg.pw_type) {
  436. case L2TP_PWTYPE_NONE:
  437. break;
  438. case L2TP_PWTYPE_ETH_VLAN:
  439. if (!info->attrs[L2TP_ATTR_VLAN_ID]) {
  440. ret = -EINVAL;
  441. goto out;
  442. }
  443. break;
  444. case L2TP_PWTYPE_ETH:
  445. break;
  446. case L2TP_PWTYPE_PPP:
  447. case L2TP_PWTYPE_PPP_AC:
  448. break;
  449. case L2TP_PWTYPE_IP:
  450. default:
  451. ret = -EPROTONOSUPPORT;
  452. break;
  453. }
  454. ret = -EPROTONOSUPPORT;
  455. if (l2tp_nl_cmd_ops[cfg.pw_type]->session_create)
  456. ret = (*l2tp_nl_cmd_ops[cfg.pw_type]->session_create)(net, tunnel_id,
  457. session_id, peer_session_id, &cfg);
  458. out:
  459. return ret;
  460. }
  461. static int l2tp_nl_cmd_session_delete(struct sk_buff *skb, struct genl_info *info)
  462. {
  463. int ret = 0;
  464. struct l2tp_session *session;
  465. u16 pw_type;
  466. session = l2tp_nl_session_find(info);
  467. if (session == NULL) {
  468. ret = -ENODEV;
  469. goto out;
  470. }
  471. pw_type = session->pwtype;
  472. if (pw_type < __L2TP_PWTYPE_MAX)
  473. if (l2tp_nl_cmd_ops[pw_type] && l2tp_nl_cmd_ops[pw_type]->session_delete)
  474. ret = (*l2tp_nl_cmd_ops[pw_type]->session_delete)(session);
  475. out:
  476. return ret;
  477. }
  478. static int l2tp_nl_cmd_session_modify(struct sk_buff *skb, struct genl_info *info)
  479. {
  480. int ret = 0;
  481. struct l2tp_session *session;
  482. session = l2tp_nl_session_find(info);
  483. if (session == NULL) {
  484. ret = -ENODEV;
  485. goto out;
  486. }
  487. if (info->attrs[L2TP_ATTR_DEBUG])
  488. session->debug = nla_get_u32(info->attrs[L2TP_ATTR_DEBUG]);
  489. if (info->attrs[L2TP_ATTR_DATA_SEQ])
  490. session->data_seq = nla_get_u8(info->attrs[L2TP_ATTR_DATA_SEQ]);
  491. if (info->attrs[L2TP_ATTR_RECV_SEQ])
  492. session->recv_seq = nla_get_u8(info->attrs[L2TP_ATTR_RECV_SEQ]);
  493. if (info->attrs[L2TP_ATTR_SEND_SEQ])
  494. session->send_seq = nla_get_u8(info->attrs[L2TP_ATTR_SEND_SEQ]);
  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. struct l2tp_stats stats;
  514. unsigned int start;
  515. sk = tunnel->sock;
  516. hdr = genlmsg_put(skb, portid, seq, &l2tp_nl_family, flags, L2TP_CMD_SESSION_GET);
  517. if (!hdr)
  518. return -EMSGSIZE;
  519. if (nla_put_u32(skb, L2TP_ATTR_CONN_ID, tunnel->tunnel_id) ||
  520. nla_put_u32(skb, L2TP_ATTR_SESSION_ID, session->session_id) ||
  521. nla_put_u32(skb, L2TP_ATTR_PEER_CONN_ID, tunnel->peer_tunnel_id) ||
  522. nla_put_u32(skb, L2TP_ATTR_PEER_SESSION_ID,
  523. session->peer_session_id) ||
  524. nla_put_u32(skb, L2TP_ATTR_DEBUG, session->debug) ||
  525. nla_put_u16(skb, L2TP_ATTR_PW_TYPE, session->pwtype) ||
  526. nla_put_u16(skb, L2TP_ATTR_MTU, session->mtu) ||
  527. (session->mru &&
  528. nla_put_u16(skb, L2TP_ATTR_MRU, session->mru)))
  529. goto nla_put_failure;
  530. if ((session->ifname[0] &&
  531. nla_put_string(skb, L2TP_ATTR_IFNAME, session->ifname)) ||
  532. (session->cookie_len &&
  533. nla_put(skb, L2TP_ATTR_COOKIE, session->cookie_len,
  534. &session->cookie[0])) ||
  535. (session->peer_cookie_len &&
  536. nla_put(skb, L2TP_ATTR_PEER_COOKIE, session->peer_cookie_len,
  537. &session->peer_cookie[0])) ||
  538. nla_put_u8(skb, L2TP_ATTR_RECV_SEQ, session->recv_seq) ||
  539. nla_put_u8(skb, L2TP_ATTR_SEND_SEQ, session->send_seq) ||
  540. nla_put_u8(skb, L2TP_ATTR_LNS_MODE, session->lns_mode) ||
  541. #ifdef CONFIG_XFRM
  542. (((sk) && (sk->sk_policy[0] || sk->sk_policy[1])) &&
  543. nla_put_u8(skb, L2TP_ATTR_USING_IPSEC, 1)) ||
  544. #endif
  545. (session->reorder_timeout &&
  546. nla_put_msecs(skb, L2TP_ATTR_RECV_TIMEOUT, session->reorder_timeout)))
  547. goto nla_put_failure;
  548. nest = nla_nest_start(skb, L2TP_ATTR_STATS);
  549. if (nest == NULL)
  550. goto nla_put_failure;
  551. do {
  552. start = u64_stats_fetch_begin(&session->stats.syncp);
  553. stats.tx_packets = session->stats.tx_packets;
  554. stats.tx_bytes = session->stats.tx_bytes;
  555. stats.tx_errors = session->stats.tx_errors;
  556. stats.rx_packets = session->stats.rx_packets;
  557. stats.rx_bytes = session->stats.rx_bytes;
  558. stats.rx_errors = session->stats.rx_errors;
  559. stats.rx_seq_discards = session->stats.rx_seq_discards;
  560. stats.rx_oos_packets = session->stats.rx_oos_packets;
  561. } while (u64_stats_fetch_retry(&session->stats.syncp, start));
  562. if (nla_put_u64(skb, L2TP_ATTR_TX_PACKETS, stats.tx_packets) ||
  563. nla_put_u64(skb, L2TP_ATTR_TX_BYTES, stats.tx_bytes) ||
  564. nla_put_u64(skb, L2TP_ATTR_TX_ERRORS, stats.tx_errors) ||
  565. nla_put_u64(skb, L2TP_ATTR_RX_PACKETS, stats.rx_packets) ||
  566. nla_put_u64(skb, L2TP_ATTR_RX_BYTES, stats.rx_bytes) ||
  567. nla_put_u64(skb, L2TP_ATTR_RX_SEQ_DISCARDS,
  568. stats.rx_seq_discards) ||
  569. nla_put_u64(skb, L2TP_ATTR_RX_OOS_PACKETS,
  570. stats.rx_oos_packets) ||
  571. nla_put_u64(skb, L2TP_ATTR_RX_ERRORS, stats.rx_errors))
  572. goto nla_put_failure;
  573. nla_nest_end(skb, nest);
  574. return genlmsg_end(skb, hdr);
  575. nla_put_failure:
  576. genlmsg_cancel(skb, hdr);
  577. return -1;
  578. }
  579. static int l2tp_nl_cmd_session_get(struct sk_buff *skb, struct genl_info *info)
  580. {
  581. struct l2tp_session *session;
  582. struct sk_buff *msg;
  583. int ret;
  584. session = l2tp_nl_session_find(info);
  585. if (session == NULL) {
  586. ret = -ENODEV;
  587. goto out;
  588. }
  589. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  590. if (!msg) {
  591. ret = -ENOMEM;
  592. goto out;
  593. }
  594. ret = l2tp_nl_session_send(msg, info->snd_portid, info->snd_seq,
  595. 0, session);
  596. if (ret < 0)
  597. goto err_out;
  598. return genlmsg_unicast(genl_info_net(info), msg, info->snd_portid);
  599. err_out:
  600. nlmsg_free(msg);
  601. out:
  602. return ret;
  603. }
  604. static int l2tp_nl_cmd_session_dump(struct sk_buff *skb, struct netlink_callback *cb)
  605. {
  606. struct net *net = sock_net(skb->sk);
  607. struct l2tp_session *session;
  608. struct l2tp_tunnel *tunnel = NULL;
  609. int ti = cb->args[0];
  610. int si = cb->args[1];
  611. for (;;) {
  612. if (tunnel == NULL) {
  613. tunnel = l2tp_tunnel_find_nth(net, ti);
  614. if (tunnel == NULL)
  615. goto out;
  616. }
  617. session = l2tp_session_find_nth(tunnel, si);
  618. if (session == NULL) {
  619. ti++;
  620. tunnel = NULL;
  621. si = 0;
  622. continue;
  623. }
  624. if (l2tp_nl_session_send(skb, NETLINK_CB(cb->skb).portid,
  625. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  626. session) <= 0)
  627. break;
  628. si++;
  629. }
  630. out:
  631. cb->args[0] = ti;
  632. cb->args[1] = si;
  633. return skb->len;
  634. }
  635. static struct nla_policy l2tp_nl_policy[L2TP_ATTR_MAX + 1] = {
  636. [L2TP_ATTR_NONE] = { .type = NLA_UNSPEC, },
  637. [L2TP_ATTR_PW_TYPE] = { .type = NLA_U16, },
  638. [L2TP_ATTR_ENCAP_TYPE] = { .type = NLA_U16, },
  639. [L2TP_ATTR_OFFSET] = { .type = NLA_U16, },
  640. [L2TP_ATTR_DATA_SEQ] = { .type = NLA_U8, },
  641. [L2TP_ATTR_L2SPEC_TYPE] = { .type = NLA_U8, },
  642. [L2TP_ATTR_L2SPEC_LEN] = { .type = NLA_U8, },
  643. [L2TP_ATTR_PROTO_VERSION] = { .type = NLA_U8, },
  644. [L2TP_ATTR_CONN_ID] = { .type = NLA_U32, },
  645. [L2TP_ATTR_PEER_CONN_ID] = { .type = NLA_U32, },
  646. [L2TP_ATTR_SESSION_ID] = { .type = NLA_U32, },
  647. [L2TP_ATTR_PEER_SESSION_ID] = { .type = NLA_U32, },
  648. [L2TP_ATTR_UDP_CSUM] = { .type = NLA_U8, },
  649. [L2TP_ATTR_VLAN_ID] = { .type = NLA_U16, },
  650. [L2TP_ATTR_DEBUG] = { .type = NLA_U32, },
  651. [L2TP_ATTR_RECV_SEQ] = { .type = NLA_U8, },
  652. [L2TP_ATTR_SEND_SEQ] = { .type = NLA_U8, },
  653. [L2TP_ATTR_LNS_MODE] = { .type = NLA_U8, },
  654. [L2TP_ATTR_USING_IPSEC] = { .type = NLA_U8, },
  655. [L2TP_ATTR_RECV_TIMEOUT] = { .type = NLA_MSECS, },
  656. [L2TP_ATTR_FD] = { .type = NLA_U32, },
  657. [L2TP_ATTR_IP_SADDR] = { .type = NLA_U32, },
  658. [L2TP_ATTR_IP_DADDR] = { .type = NLA_U32, },
  659. [L2TP_ATTR_UDP_SPORT] = { .type = NLA_U16, },
  660. [L2TP_ATTR_UDP_DPORT] = { .type = NLA_U16, },
  661. [L2TP_ATTR_MTU] = { .type = NLA_U16, },
  662. [L2TP_ATTR_MRU] = { .type = NLA_U16, },
  663. [L2TP_ATTR_STATS] = { .type = NLA_NESTED, },
  664. [L2TP_ATTR_IP6_SADDR] = {
  665. .type = NLA_BINARY,
  666. .len = sizeof(struct in6_addr),
  667. },
  668. [L2TP_ATTR_IP6_DADDR] = {
  669. .type = NLA_BINARY,
  670. .len = sizeof(struct in6_addr),
  671. },
  672. [L2TP_ATTR_IFNAME] = {
  673. .type = NLA_NUL_STRING,
  674. .len = IFNAMSIZ - 1,
  675. },
  676. [L2TP_ATTR_COOKIE] = {
  677. .type = NLA_BINARY,
  678. .len = 8,
  679. },
  680. [L2TP_ATTR_PEER_COOKIE] = {
  681. .type = NLA_BINARY,
  682. .len = 8,
  683. },
  684. };
  685. static struct genl_ops l2tp_nl_ops[] = {
  686. {
  687. .cmd = L2TP_CMD_NOOP,
  688. .doit = l2tp_nl_cmd_noop,
  689. .policy = l2tp_nl_policy,
  690. /* can be retrieved by unprivileged users */
  691. },
  692. {
  693. .cmd = L2TP_CMD_TUNNEL_CREATE,
  694. .doit = l2tp_nl_cmd_tunnel_create,
  695. .policy = l2tp_nl_policy,
  696. .flags = GENL_ADMIN_PERM,
  697. },
  698. {
  699. .cmd = L2TP_CMD_TUNNEL_DELETE,
  700. .doit = l2tp_nl_cmd_tunnel_delete,
  701. .policy = l2tp_nl_policy,
  702. .flags = GENL_ADMIN_PERM,
  703. },
  704. {
  705. .cmd = L2TP_CMD_TUNNEL_MODIFY,
  706. .doit = l2tp_nl_cmd_tunnel_modify,
  707. .policy = l2tp_nl_policy,
  708. .flags = GENL_ADMIN_PERM,
  709. },
  710. {
  711. .cmd = L2TP_CMD_TUNNEL_GET,
  712. .doit = l2tp_nl_cmd_tunnel_get,
  713. .dumpit = l2tp_nl_cmd_tunnel_dump,
  714. .policy = l2tp_nl_policy,
  715. .flags = GENL_ADMIN_PERM,
  716. },
  717. {
  718. .cmd = L2TP_CMD_SESSION_CREATE,
  719. .doit = l2tp_nl_cmd_session_create,
  720. .policy = l2tp_nl_policy,
  721. .flags = GENL_ADMIN_PERM,
  722. },
  723. {
  724. .cmd = L2TP_CMD_SESSION_DELETE,
  725. .doit = l2tp_nl_cmd_session_delete,
  726. .policy = l2tp_nl_policy,
  727. .flags = GENL_ADMIN_PERM,
  728. },
  729. {
  730. .cmd = L2TP_CMD_SESSION_MODIFY,
  731. .doit = l2tp_nl_cmd_session_modify,
  732. .policy = l2tp_nl_policy,
  733. .flags = GENL_ADMIN_PERM,
  734. },
  735. {
  736. .cmd = L2TP_CMD_SESSION_GET,
  737. .doit = l2tp_nl_cmd_session_get,
  738. .dumpit = l2tp_nl_cmd_session_dump,
  739. .policy = l2tp_nl_policy,
  740. .flags = GENL_ADMIN_PERM,
  741. },
  742. };
  743. int l2tp_nl_register_ops(enum l2tp_pwtype pw_type, const struct l2tp_nl_cmd_ops *ops)
  744. {
  745. int ret;
  746. ret = -EINVAL;
  747. if (pw_type >= __L2TP_PWTYPE_MAX)
  748. goto err;
  749. genl_lock();
  750. ret = -EBUSY;
  751. if (l2tp_nl_cmd_ops[pw_type])
  752. goto out;
  753. l2tp_nl_cmd_ops[pw_type] = ops;
  754. ret = 0;
  755. out:
  756. genl_unlock();
  757. err:
  758. return ret;
  759. }
  760. EXPORT_SYMBOL_GPL(l2tp_nl_register_ops);
  761. void l2tp_nl_unregister_ops(enum l2tp_pwtype pw_type)
  762. {
  763. if (pw_type < __L2TP_PWTYPE_MAX) {
  764. genl_lock();
  765. l2tp_nl_cmd_ops[pw_type] = NULL;
  766. genl_unlock();
  767. }
  768. }
  769. EXPORT_SYMBOL_GPL(l2tp_nl_unregister_ops);
  770. static int l2tp_nl_init(void)
  771. {
  772. int err;
  773. pr_info("L2TP netlink interface\n");
  774. err = genl_register_family_with_ops(&l2tp_nl_family, l2tp_nl_ops,
  775. ARRAY_SIZE(l2tp_nl_ops));
  776. return err;
  777. }
  778. static void l2tp_nl_cleanup(void)
  779. {
  780. genl_unregister_family(&l2tp_nl_family);
  781. }
  782. module_init(l2tp_nl_init);
  783. module_exit(l2tp_nl_cleanup);
  784. MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
  785. MODULE_DESCRIPTION("L2TP netlink");
  786. MODULE_LICENSE("GPL");
  787. MODULE_VERSION("1.0");
  788. MODULE_ALIAS_GENL_FAMILY("l2tp");