l2tp_netlink.c 25 KB

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