l2tp_netlink.c 23 KB

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