l2tp_netlink.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925
  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. #include <net/sock.h>
  17. #include <net/genetlink.h>
  18. #include <net/udp.h>
  19. #include <linux/in.h>
  20. #include <linux/udp.h>
  21. #include <linux/socket.h>
  22. #include <linux/module.h>
  23. #include <linux/list.h>
  24. #include <net/net_namespace.h>
  25. #include <linux/l2tp.h>
  26. #include "l2tp_core.h"
  27. static struct genl_family l2tp_nl_family = {
  28. .id = GENL_ID_GENERATE,
  29. .name = L2TP_GENL_NAME,
  30. .version = L2TP_GENL_VERSION,
  31. .hdrsize = 0,
  32. .maxattr = L2TP_ATTR_MAX,
  33. };
  34. /* Accessed under genl lock */
  35. static const struct l2tp_nl_cmd_ops *l2tp_nl_cmd_ops[__L2TP_PWTYPE_MAX];
  36. static struct l2tp_session *l2tp_nl_session_find(struct genl_info *info)
  37. {
  38. u32 tunnel_id;
  39. u32 session_id;
  40. char *ifname;
  41. struct l2tp_tunnel *tunnel;
  42. struct l2tp_session *session = NULL;
  43. struct net *net = genl_info_net(info);
  44. if (info->attrs[L2TP_ATTR_IFNAME]) {
  45. ifname = nla_data(info->attrs[L2TP_ATTR_IFNAME]);
  46. session = l2tp_session_find_by_ifname(net, ifname);
  47. } else if ((info->attrs[L2TP_ATTR_SESSION_ID]) &&
  48. (info->attrs[L2TP_ATTR_CONN_ID])) {
  49. tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
  50. session_id = nla_get_u32(info->attrs[L2TP_ATTR_SESSION_ID]);
  51. tunnel = l2tp_tunnel_find(net, tunnel_id);
  52. if (tunnel)
  53. session = l2tp_session_find(net, tunnel, session_id);
  54. }
  55. return session;
  56. }
  57. static int l2tp_nl_cmd_noop(struct sk_buff *skb, struct genl_info *info)
  58. {
  59. struct sk_buff *msg;
  60. void *hdr;
  61. int ret = -ENOBUFS;
  62. msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  63. if (!msg) {
  64. ret = -ENOMEM;
  65. goto out;
  66. }
  67. hdr = genlmsg_put(msg, info->snd_pid, info->snd_seq,
  68. &l2tp_nl_family, 0, L2TP_CMD_NOOP);
  69. if (IS_ERR(hdr)) {
  70. ret = PTR_ERR(hdr);
  71. goto err_out;
  72. }
  73. genlmsg_end(msg, hdr);
  74. return genlmsg_unicast(genl_info_net(info), msg, info->snd_pid);
  75. err_out:
  76. nlmsg_free(msg);
  77. out:
  78. return ret;
  79. }
  80. static int l2tp_nl_cmd_tunnel_create(struct sk_buff *skb, struct genl_info *info)
  81. {
  82. u32 tunnel_id;
  83. u32 peer_tunnel_id;
  84. int proto_version;
  85. int fd;
  86. int ret = 0;
  87. struct l2tp_tunnel_cfg cfg = { 0, };
  88. struct l2tp_tunnel *tunnel;
  89. struct net *net = genl_info_net(info);
  90. if (!info->attrs[L2TP_ATTR_CONN_ID]) {
  91. ret = -EINVAL;
  92. goto out;
  93. }
  94. tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
  95. if (!info->attrs[L2TP_ATTR_PEER_CONN_ID]) {
  96. ret = -EINVAL;
  97. goto out;
  98. }
  99. peer_tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_PEER_CONN_ID]);
  100. if (!info->attrs[L2TP_ATTR_PROTO_VERSION]) {
  101. ret = -EINVAL;
  102. goto out;
  103. }
  104. proto_version = nla_get_u8(info->attrs[L2TP_ATTR_PROTO_VERSION]);
  105. if (!info->attrs[L2TP_ATTR_ENCAP_TYPE]) {
  106. ret = -EINVAL;
  107. goto out;
  108. }
  109. cfg.encap = nla_get_u16(info->attrs[L2TP_ATTR_ENCAP_TYPE]);
  110. fd = -1;
  111. if (info->attrs[L2TP_ATTR_FD]) {
  112. fd = nla_get_u32(info->attrs[L2TP_ATTR_FD]);
  113. } else {
  114. #if IS_ENABLED(CONFIG_IPV6)
  115. if (info->attrs[L2TP_ATTR_IP6_SADDR] &&
  116. info->attrs[L2TP_ATTR_IP6_DADDR]) {
  117. cfg.local_ip6 = nla_data(
  118. info->attrs[L2TP_ATTR_IP6_SADDR]);
  119. cfg.peer_ip6 = nla_data(
  120. info->attrs[L2TP_ATTR_IP6_DADDR]);
  121. } else
  122. #endif
  123. if (info->attrs[L2TP_ATTR_IP_SADDR] &&
  124. info->attrs[L2TP_ATTR_IP_DADDR]) {
  125. cfg.local_ip.s_addr = nla_get_be32(
  126. info->attrs[L2TP_ATTR_IP_SADDR]);
  127. cfg.peer_ip.s_addr = nla_get_be32(
  128. info->attrs[L2TP_ATTR_IP_DADDR]);
  129. } else {
  130. ret = -EINVAL;
  131. goto out;
  132. }
  133. if (info->attrs[L2TP_ATTR_UDP_SPORT])
  134. cfg.local_udp_port = nla_get_u16(info->attrs[L2TP_ATTR_UDP_SPORT]);
  135. if (info->attrs[L2TP_ATTR_UDP_DPORT])
  136. cfg.peer_udp_port = nla_get_u16(info->attrs[L2TP_ATTR_UDP_DPORT]);
  137. if (info->attrs[L2TP_ATTR_UDP_CSUM])
  138. cfg.use_udp_checksums = nla_get_flag(info->attrs[L2TP_ATTR_UDP_CSUM]);
  139. }
  140. if (info->attrs[L2TP_ATTR_DEBUG])
  141. cfg.debug = nla_get_u32(info->attrs[L2TP_ATTR_DEBUG]);
  142. tunnel = l2tp_tunnel_find(net, tunnel_id);
  143. if (tunnel != NULL) {
  144. ret = -EEXIST;
  145. goto out;
  146. }
  147. ret = -EINVAL;
  148. switch (cfg.encap) {
  149. case L2TP_ENCAPTYPE_UDP:
  150. case L2TP_ENCAPTYPE_IP:
  151. ret = l2tp_tunnel_create(net, fd, proto_version, tunnel_id,
  152. peer_tunnel_id, &cfg, &tunnel);
  153. break;
  154. }
  155. out:
  156. return ret;
  157. }
  158. static int l2tp_nl_cmd_tunnel_delete(struct sk_buff *skb, struct genl_info *info)
  159. {
  160. struct l2tp_tunnel *tunnel;
  161. u32 tunnel_id;
  162. int ret = 0;
  163. struct net *net = genl_info_net(info);
  164. if (!info->attrs[L2TP_ATTR_CONN_ID]) {
  165. ret = -EINVAL;
  166. goto out;
  167. }
  168. tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
  169. tunnel = l2tp_tunnel_find(net, tunnel_id);
  170. if (tunnel == NULL) {
  171. ret = -ENODEV;
  172. goto out;
  173. }
  174. (void) l2tp_tunnel_delete(tunnel);
  175. out:
  176. return ret;
  177. }
  178. static int l2tp_nl_cmd_tunnel_modify(struct sk_buff *skb, struct genl_info *info)
  179. {
  180. struct l2tp_tunnel *tunnel;
  181. u32 tunnel_id;
  182. int ret = 0;
  183. struct net *net = genl_info_net(info);
  184. if (!info->attrs[L2TP_ATTR_CONN_ID]) {
  185. ret = -EINVAL;
  186. goto out;
  187. }
  188. tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
  189. tunnel = l2tp_tunnel_find(net, tunnel_id);
  190. if (tunnel == NULL) {
  191. ret = -ENODEV;
  192. goto out;
  193. }
  194. if (info->attrs[L2TP_ATTR_DEBUG])
  195. tunnel->debug = nla_get_u32(info->attrs[L2TP_ATTR_DEBUG]);
  196. out:
  197. return ret;
  198. }
  199. static int l2tp_nl_tunnel_send(struct sk_buff *skb, u32 pid, u32 seq, int flags,
  200. struct l2tp_tunnel *tunnel)
  201. {
  202. void *hdr;
  203. struct nlattr *nest;
  204. struct sock *sk = NULL;
  205. struct inet_sock *inet;
  206. #if IS_ENABLED(CONFIG_IPV6)
  207. struct ipv6_pinfo *np = NULL;
  208. #endif
  209. struct l2tp_stats stats;
  210. unsigned int start;
  211. hdr = genlmsg_put(skb, pid, seq, &l2tp_nl_family, flags,
  212. L2TP_CMD_TUNNEL_GET);
  213. if (IS_ERR(hdr))
  214. return PTR_ERR(hdr);
  215. if (nla_put_u8(skb, L2TP_ATTR_PROTO_VERSION, tunnel->version) ||
  216. nla_put_u32(skb, L2TP_ATTR_CONN_ID, tunnel->tunnel_id) ||
  217. nla_put_u32(skb, L2TP_ATTR_PEER_CONN_ID, tunnel->peer_tunnel_id) ||
  218. nla_put_u32(skb, L2TP_ATTR_DEBUG, tunnel->debug) ||
  219. nla_put_u16(skb, L2TP_ATTR_ENCAP_TYPE, tunnel->encap))
  220. goto nla_put_failure;
  221. nest = nla_nest_start(skb, L2TP_ATTR_STATS);
  222. if (nest == NULL)
  223. goto nla_put_failure;
  224. do {
  225. start = u64_stats_fetch_begin(&tunnel->stats.syncp);
  226. stats.tx_packets = tunnel->stats.tx_packets;
  227. stats.tx_bytes = tunnel->stats.tx_bytes;
  228. stats.tx_errors = tunnel->stats.tx_errors;
  229. stats.rx_packets = tunnel->stats.rx_packets;
  230. stats.rx_bytes = tunnel->stats.rx_bytes;
  231. stats.rx_errors = tunnel->stats.rx_errors;
  232. stats.rx_seq_discards = tunnel->stats.rx_seq_discards;
  233. stats.rx_oos_packets = tunnel->stats.rx_oos_packets;
  234. } while (u64_stats_fetch_retry(&tunnel->stats.syncp, start));
  235. if (nla_put_u64(skb, L2TP_ATTR_TX_PACKETS, stats.tx_packets) ||
  236. nla_put_u64(skb, L2TP_ATTR_TX_BYTES, stats.tx_bytes) ||
  237. nla_put_u64(skb, L2TP_ATTR_TX_ERRORS, stats.tx_errors) ||
  238. nla_put_u64(skb, L2TP_ATTR_RX_PACKETS, stats.rx_packets) ||
  239. nla_put_u64(skb, L2TP_ATTR_RX_BYTES, stats.rx_bytes) ||
  240. nla_put_u64(skb, L2TP_ATTR_RX_SEQ_DISCARDS,
  241. stats.rx_seq_discards) ||
  242. nla_put_u64(skb, L2TP_ATTR_RX_OOS_PACKETS,
  243. stats.rx_oos_packets) ||
  244. nla_put_u64(skb, L2TP_ATTR_RX_ERRORS, stats.rx_errors))
  245. goto nla_put_failure;
  246. nla_nest_end(skb, nest);
  247. sk = tunnel->sock;
  248. if (!sk)
  249. goto out;
  250. #if IS_ENABLED(CONFIG_IPV6)
  251. if (sk->sk_family == AF_INET6)
  252. np = inet6_sk(sk);
  253. #endif
  254. inet = inet_sk(sk);
  255. switch (tunnel->encap) {
  256. case L2TP_ENCAPTYPE_UDP:
  257. if (nla_put_u16(skb, L2TP_ATTR_UDP_SPORT, ntohs(inet->inet_sport)) ||
  258. nla_put_u16(skb, L2TP_ATTR_UDP_DPORT, ntohs(inet->inet_dport)) ||
  259. nla_put_u8(skb, L2TP_ATTR_UDP_CSUM,
  260. (sk->sk_no_check != UDP_CSUM_NOXMIT)))
  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(np->daddr),
  269. &np->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_GOODSIZE, GFP_KERNEL);
  302. if (!msg) {
  303. ret = -ENOMEM;
  304. goto out;
  305. }
  306. ret = l2tp_nl_tunnel_send(msg, info->snd_pid, info->snd_seq,
  307. NLM_F_ACK, tunnel);
  308. if (ret < 0)
  309. goto err_out;
  310. return genlmsg_unicast(net, msg, info->snd_pid);
  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).pid,
  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. if (info->attrs[L2TP_ATTR_LNS_MODE])
  494. session->lns_mode = nla_get_u8(info->attrs[L2TP_ATTR_LNS_MODE]);
  495. if (info->attrs[L2TP_ATTR_RECV_TIMEOUT])
  496. session->reorder_timeout = nla_get_msecs(info->attrs[L2TP_ATTR_RECV_TIMEOUT]);
  497. if (info->attrs[L2TP_ATTR_MTU])
  498. session->mtu = nla_get_u16(info->attrs[L2TP_ATTR_MTU]);
  499. if (info->attrs[L2TP_ATTR_MRU])
  500. session->mru = nla_get_u16(info->attrs[L2TP_ATTR_MRU]);
  501. out:
  502. return ret;
  503. }
  504. static int l2tp_nl_session_send(struct sk_buff *skb, u32 pid, u32 seq, int flags,
  505. struct l2tp_session *session)
  506. {
  507. void *hdr;
  508. struct nlattr *nest;
  509. struct l2tp_tunnel *tunnel = session->tunnel;
  510. struct sock *sk = NULL;
  511. struct l2tp_stats stats;
  512. unsigned int start;
  513. sk = tunnel->sock;
  514. hdr = genlmsg_put(skb, pid, seq, &l2tp_nl_family, flags, L2TP_CMD_SESSION_GET);
  515. if (IS_ERR(hdr))
  516. return PTR_ERR(hdr);
  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 && 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. do {
  550. start = u64_stats_fetch_begin(&session->stats.syncp);
  551. stats.tx_packets = session->stats.tx_packets;
  552. stats.tx_bytes = session->stats.tx_bytes;
  553. stats.tx_errors = session->stats.tx_errors;
  554. stats.rx_packets = session->stats.rx_packets;
  555. stats.rx_bytes = session->stats.rx_bytes;
  556. stats.rx_errors = session->stats.rx_errors;
  557. stats.rx_seq_discards = session->stats.rx_seq_discards;
  558. stats.rx_oos_packets = session->stats.rx_oos_packets;
  559. } while (u64_stats_fetch_retry(&session->stats.syncp, start));
  560. if (nla_put_u64(skb, L2TP_ATTR_TX_PACKETS, stats.tx_packets) ||
  561. nla_put_u64(skb, L2TP_ATTR_TX_BYTES, stats.tx_bytes) ||
  562. nla_put_u64(skb, L2TP_ATTR_TX_ERRORS, stats.tx_errors) ||
  563. nla_put_u64(skb, L2TP_ATTR_RX_PACKETS, stats.rx_packets) ||
  564. nla_put_u64(skb, L2TP_ATTR_RX_BYTES, stats.rx_bytes) ||
  565. nla_put_u64(skb, L2TP_ATTR_RX_SEQ_DISCARDS,
  566. stats.rx_seq_discards) ||
  567. nla_put_u64(skb, L2TP_ATTR_RX_OOS_PACKETS,
  568. stats.rx_oos_packets) ||
  569. nla_put_u64(skb, L2TP_ATTR_RX_ERRORS, stats.rx_errors))
  570. goto nla_put_failure;
  571. nla_nest_end(skb, nest);
  572. return genlmsg_end(skb, hdr);
  573. nla_put_failure:
  574. genlmsg_cancel(skb, hdr);
  575. return -1;
  576. }
  577. static int l2tp_nl_cmd_session_get(struct sk_buff *skb, struct genl_info *info)
  578. {
  579. struct l2tp_session *session;
  580. struct sk_buff *msg;
  581. int ret;
  582. session = l2tp_nl_session_find(info);
  583. if (session == NULL) {
  584. ret = -ENODEV;
  585. goto out;
  586. }
  587. msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  588. if (!msg) {
  589. ret = -ENOMEM;
  590. goto out;
  591. }
  592. ret = l2tp_nl_session_send(msg, info->snd_pid, info->snd_seq,
  593. 0, session);
  594. if (ret < 0)
  595. goto err_out;
  596. return genlmsg_unicast(genl_info_net(info), msg, info->snd_pid);
  597. err_out:
  598. nlmsg_free(msg);
  599. out:
  600. return ret;
  601. }
  602. static int l2tp_nl_cmd_session_dump(struct sk_buff *skb, struct netlink_callback *cb)
  603. {
  604. struct net *net = sock_net(skb->sk);
  605. struct l2tp_session *session;
  606. struct l2tp_tunnel *tunnel = NULL;
  607. int ti = cb->args[0];
  608. int si = cb->args[1];
  609. for (;;) {
  610. if (tunnel == NULL) {
  611. tunnel = l2tp_tunnel_find_nth(net, ti);
  612. if (tunnel == NULL)
  613. goto out;
  614. }
  615. session = l2tp_session_find_nth(tunnel, si);
  616. if (session == NULL) {
  617. ti++;
  618. tunnel = NULL;
  619. si = 0;
  620. continue;
  621. }
  622. if (l2tp_nl_session_send(skb, NETLINK_CB(cb->skb).pid,
  623. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  624. session) <= 0)
  625. break;
  626. si++;
  627. }
  628. out:
  629. cb->args[0] = ti;
  630. cb->args[1] = si;
  631. return skb->len;
  632. }
  633. static struct nla_policy l2tp_nl_policy[L2TP_ATTR_MAX + 1] = {
  634. [L2TP_ATTR_NONE] = { .type = NLA_UNSPEC, },
  635. [L2TP_ATTR_PW_TYPE] = { .type = NLA_U16, },
  636. [L2TP_ATTR_ENCAP_TYPE] = { .type = NLA_U16, },
  637. [L2TP_ATTR_OFFSET] = { .type = NLA_U16, },
  638. [L2TP_ATTR_DATA_SEQ] = { .type = NLA_U8, },
  639. [L2TP_ATTR_L2SPEC_TYPE] = { .type = NLA_U8, },
  640. [L2TP_ATTR_L2SPEC_LEN] = { .type = NLA_U8, },
  641. [L2TP_ATTR_PROTO_VERSION] = { .type = NLA_U8, },
  642. [L2TP_ATTR_CONN_ID] = { .type = NLA_U32, },
  643. [L2TP_ATTR_PEER_CONN_ID] = { .type = NLA_U32, },
  644. [L2TP_ATTR_SESSION_ID] = { .type = NLA_U32, },
  645. [L2TP_ATTR_PEER_SESSION_ID] = { .type = NLA_U32, },
  646. [L2TP_ATTR_UDP_CSUM] = { .type = NLA_U8, },
  647. [L2TP_ATTR_VLAN_ID] = { .type = NLA_U16, },
  648. [L2TP_ATTR_DEBUG] = { .type = NLA_U32, },
  649. [L2TP_ATTR_RECV_SEQ] = { .type = NLA_U8, },
  650. [L2TP_ATTR_SEND_SEQ] = { .type = NLA_U8, },
  651. [L2TP_ATTR_LNS_MODE] = { .type = NLA_U8, },
  652. [L2TP_ATTR_USING_IPSEC] = { .type = NLA_U8, },
  653. [L2TP_ATTR_RECV_TIMEOUT] = { .type = NLA_MSECS, },
  654. [L2TP_ATTR_FD] = { .type = NLA_U32, },
  655. [L2TP_ATTR_IP_SADDR] = { .type = NLA_U32, },
  656. [L2TP_ATTR_IP_DADDR] = { .type = NLA_U32, },
  657. [L2TP_ATTR_UDP_SPORT] = { .type = NLA_U16, },
  658. [L2TP_ATTR_UDP_DPORT] = { .type = NLA_U16, },
  659. [L2TP_ATTR_MTU] = { .type = NLA_U16, },
  660. [L2TP_ATTR_MRU] = { .type = NLA_U16, },
  661. [L2TP_ATTR_STATS] = { .type = NLA_NESTED, },
  662. [L2TP_ATTR_IP6_SADDR] = {
  663. .type = NLA_BINARY,
  664. .len = sizeof(struct in6_addr),
  665. },
  666. [L2TP_ATTR_IP6_DADDR] = {
  667. .type = NLA_BINARY,
  668. .len = sizeof(struct in6_addr),
  669. },
  670. [L2TP_ATTR_IFNAME] = {
  671. .type = NLA_NUL_STRING,
  672. .len = IFNAMSIZ - 1,
  673. },
  674. [L2TP_ATTR_COOKIE] = {
  675. .type = NLA_BINARY,
  676. .len = 8,
  677. },
  678. [L2TP_ATTR_PEER_COOKIE] = {
  679. .type = NLA_BINARY,
  680. .len = 8,
  681. },
  682. };
  683. static struct genl_ops l2tp_nl_ops[] = {
  684. {
  685. .cmd = L2TP_CMD_NOOP,
  686. .doit = l2tp_nl_cmd_noop,
  687. .policy = l2tp_nl_policy,
  688. /* can be retrieved by unprivileged users */
  689. },
  690. {
  691. .cmd = L2TP_CMD_TUNNEL_CREATE,
  692. .doit = l2tp_nl_cmd_tunnel_create,
  693. .policy = l2tp_nl_policy,
  694. .flags = GENL_ADMIN_PERM,
  695. },
  696. {
  697. .cmd = L2TP_CMD_TUNNEL_DELETE,
  698. .doit = l2tp_nl_cmd_tunnel_delete,
  699. .policy = l2tp_nl_policy,
  700. .flags = GENL_ADMIN_PERM,
  701. },
  702. {
  703. .cmd = L2TP_CMD_TUNNEL_MODIFY,
  704. .doit = l2tp_nl_cmd_tunnel_modify,
  705. .policy = l2tp_nl_policy,
  706. .flags = GENL_ADMIN_PERM,
  707. },
  708. {
  709. .cmd = L2TP_CMD_TUNNEL_GET,
  710. .doit = l2tp_nl_cmd_tunnel_get,
  711. .dumpit = l2tp_nl_cmd_tunnel_dump,
  712. .policy = l2tp_nl_policy,
  713. .flags = GENL_ADMIN_PERM,
  714. },
  715. {
  716. .cmd = L2TP_CMD_SESSION_CREATE,
  717. .doit = l2tp_nl_cmd_session_create,
  718. .policy = l2tp_nl_policy,
  719. .flags = GENL_ADMIN_PERM,
  720. },
  721. {
  722. .cmd = L2TP_CMD_SESSION_DELETE,
  723. .doit = l2tp_nl_cmd_session_delete,
  724. .policy = l2tp_nl_policy,
  725. .flags = GENL_ADMIN_PERM,
  726. },
  727. {
  728. .cmd = L2TP_CMD_SESSION_MODIFY,
  729. .doit = l2tp_nl_cmd_session_modify,
  730. .policy = l2tp_nl_policy,
  731. .flags = GENL_ADMIN_PERM,
  732. },
  733. {
  734. .cmd = L2TP_CMD_SESSION_GET,
  735. .doit = l2tp_nl_cmd_session_get,
  736. .dumpit = l2tp_nl_cmd_session_dump,
  737. .policy = l2tp_nl_policy,
  738. .flags = GENL_ADMIN_PERM,
  739. },
  740. };
  741. int l2tp_nl_register_ops(enum l2tp_pwtype pw_type, const struct l2tp_nl_cmd_ops *ops)
  742. {
  743. int ret;
  744. ret = -EINVAL;
  745. if (pw_type >= __L2TP_PWTYPE_MAX)
  746. goto err;
  747. genl_lock();
  748. ret = -EBUSY;
  749. if (l2tp_nl_cmd_ops[pw_type])
  750. goto out;
  751. l2tp_nl_cmd_ops[pw_type] = ops;
  752. ret = 0;
  753. out:
  754. genl_unlock();
  755. err:
  756. return ret;
  757. }
  758. EXPORT_SYMBOL_GPL(l2tp_nl_register_ops);
  759. void l2tp_nl_unregister_ops(enum l2tp_pwtype pw_type)
  760. {
  761. if (pw_type < __L2TP_PWTYPE_MAX) {
  762. genl_lock();
  763. l2tp_nl_cmd_ops[pw_type] = NULL;
  764. genl_unlock();
  765. }
  766. }
  767. EXPORT_SYMBOL_GPL(l2tp_nl_unregister_ops);
  768. static int l2tp_nl_init(void)
  769. {
  770. int err;
  771. printk(KERN_INFO "L2TP netlink interface\n");
  772. err = genl_register_family_with_ops(&l2tp_nl_family, l2tp_nl_ops,
  773. ARRAY_SIZE(l2tp_nl_ops));
  774. return err;
  775. }
  776. static void l2tp_nl_cleanup(void)
  777. {
  778. genl_unregister_family(&l2tp_nl_family);
  779. }
  780. module_init(l2tp_nl_init);
  781. module_exit(l2tp_nl_cleanup);
  782. MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
  783. MODULE_DESCRIPTION("L2TP netlink");
  784. MODULE_LICENSE("GPL");
  785. MODULE_VERSION("1.0");
  786. MODULE_ALIAS("net-pf-" __stringify(PF_NETLINK) "-proto-" \
  787. __stringify(NETLINK_GENERIC) "-type-" "l2tp");