l2tp_netlink.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  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. return genlmsg_end(skb, hdr);
  330. nla_put_failure:
  331. genlmsg_cancel(skb, hdr);
  332. return -1;
  333. }
  334. static int l2tp_nl_cmd_tunnel_get(struct sk_buff *skb, struct genl_info *info)
  335. {
  336. struct l2tp_tunnel *tunnel;
  337. struct sk_buff *msg;
  338. u32 tunnel_id;
  339. int ret = -ENOBUFS;
  340. struct net *net = genl_info_net(info);
  341. if (!info->attrs[L2TP_ATTR_CONN_ID]) {
  342. ret = -EINVAL;
  343. goto out;
  344. }
  345. tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
  346. tunnel = l2tp_tunnel_find(net, tunnel_id);
  347. if (tunnel == NULL) {
  348. ret = -ENODEV;
  349. goto out;
  350. }
  351. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  352. if (!msg) {
  353. ret = -ENOMEM;
  354. goto out;
  355. }
  356. ret = l2tp_nl_tunnel_send(msg, info->snd_portid, info->snd_seq,
  357. NLM_F_ACK, tunnel, L2TP_CMD_TUNNEL_GET);
  358. if (ret < 0)
  359. goto err_out;
  360. return genlmsg_unicast(net, msg, info->snd_portid);
  361. err_out:
  362. nlmsg_free(msg);
  363. out:
  364. return ret;
  365. }
  366. static int l2tp_nl_cmd_tunnel_dump(struct sk_buff *skb, struct netlink_callback *cb)
  367. {
  368. int ti = cb->args[0];
  369. struct l2tp_tunnel *tunnel;
  370. struct net *net = sock_net(skb->sk);
  371. for (;;) {
  372. tunnel = l2tp_tunnel_find_nth(net, ti);
  373. if (tunnel == NULL)
  374. goto out;
  375. if (l2tp_nl_tunnel_send(skb, NETLINK_CB(cb->skb).portid,
  376. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  377. tunnel, L2TP_CMD_TUNNEL_GET) <= 0)
  378. goto out;
  379. ti++;
  380. }
  381. out:
  382. cb->args[0] = ti;
  383. return skb->len;
  384. }
  385. static int l2tp_nl_cmd_session_create(struct sk_buff *skb, struct genl_info *info)
  386. {
  387. u32 tunnel_id = 0;
  388. u32 session_id;
  389. u32 peer_session_id;
  390. int ret = 0;
  391. struct l2tp_tunnel *tunnel;
  392. struct l2tp_session *session;
  393. struct l2tp_session_cfg cfg = { 0, };
  394. struct net *net = genl_info_net(info);
  395. if (!info->attrs[L2TP_ATTR_CONN_ID]) {
  396. ret = -EINVAL;
  397. goto out;
  398. }
  399. tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
  400. tunnel = l2tp_tunnel_find(net, tunnel_id);
  401. if (!tunnel) {
  402. ret = -ENODEV;
  403. goto out;
  404. }
  405. if (!info->attrs[L2TP_ATTR_SESSION_ID]) {
  406. ret = -EINVAL;
  407. goto out;
  408. }
  409. session_id = nla_get_u32(info->attrs[L2TP_ATTR_SESSION_ID]);
  410. session = l2tp_session_find(net, tunnel, session_id);
  411. if (session) {
  412. ret = -EEXIST;
  413. goto out;
  414. }
  415. if (!info->attrs[L2TP_ATTR_PEER_SESSION_ID]) {
  416. ret = -EINVAL;
  417. goto out;
  418. }
  419. peer_session_id = nla_get_u32(info->attrs[L2TP_ATTR_PEER_SESSION_ID]);
  420. if (!info->attrs[L2TP_ATTR_PW_TYPE]) {
  421. ret = -EINVAL;
  422. goto out;
  423. }
  424. cfg.pw_type = nla_get_u16(info->attrs[L2TP_ATTR_PW_TYPE]);
  425. if (cfg.pw_type >= __L2TP_PWTYPE_MAX) {
  426. ret = -EINVAL;
  427. goto out;
  428. }
  429. if (tunnel->version > 2) {
  430. if (info->attrs[L2TP_ATTR_OFFSET])
  431. cfg.offset = nla_get_u16(info->attrs[L2TP_ATTR_OFFSET]);
  432. if (info->attrs[L2TP_ATTR_DATA_SEQ])
  433. cfg.data_seq = nla_get_u8(info->attrs[L2TP_ATTR_DATA_SEQ]);
  434. cfg.l2specific_type = L2TP_L2SPECTYPE_DEFAULT;
  435. if (info->attrs[L2TP_ATTR_L2SPEC_TYPE])
  436. cfg.l2specific_type = nla_get_u8(info->attrs[L2TP_ATTR_L2SPEC_TYPE]);
  437. cfg.l2specific_len = 4;
  438. if (info->attrs[L2TP_ATTR_L2SPEC_LEN])
  439. cfg.l2specific_len = nla_get_u8(info->attrs[L2TP_ATTR_L2SPEC_LEN]);
  440. if (info->attrs[L2TP_ATTR_COOKIE]) {
  441. u16 len = nla_len(info->attrs[L2TP_ATTR_COOKIE]);
  442. if (len > 8) {
  443. ret = -EINVAL;
  444. goto out;
  445. }
  446. cfg.cookie_len = len;
  447. memcpy(&cfg.cookie[0], nla_data(info->attrs[L2TP_ATTR_COOKIE]), len);
  448. }
  449. if (info->attrs[L2TP_ATTR_PEER_COOKIE]) {
  450. u16 len = nla_len(info->attrs[L2TP_ATTR_PEER_COOKIE]);
  451. if (len > 8) {
  452. ret = -EINVAL;
  453. goto out;
  454. }
  455. cfg.peer_cookie_len = len;
  456. memcpy(&cfg.peer_cookie[0], nla_data(info->attrs[L2TP_ATTR_PEER_COOKIE]), len);
  457. }
  458. if (info->attrs[L2TP_ATTR_IFNAME])
  459. cfg.ifname = nla_data(info->attrs[L2TP_ATTR_IFNAME]);
  460. if (info->attrs[L2TP_ATTR_VLAN_ID])
  461. cfg.vlan_id = nla_get_u16(info->attrs[L2TP_ATTR_VLAN_ID]);
  462. }
  463. if (info->attrs[L2TP_ATTR_DEBUG])
  464. cfg.debug = nla_get_u32(info->attrs[L2TP_ATTR_DEBUG]);
  465. if (info->attrs[L2TP_ATTR_RECV_SEQ])
  466. cfg.recv_seq = nla_get_u8(info->attrs[L2TP_ATTR_RECV_SEQ]);
  467. if (info->attrs[L2TP_ATTR_SEND_SEQ])
  468. cfg.send_seq = nla_get_u8(info->attrs[L2TP_ATTR_SEND_SEQ]);
  469. if (info->attrs[L2TP_ATTR_LNS_MODE])
  470. cfg.lns_mode = nla_get_u8(info->attrs[L2TP_ATTR_LNS_MODE]);
  471. if (info->attrs[L2TP_ATTR_RECV_TIMEOUT])
  472. cfg.reorder_timeout = nla_get_msecs(info->attrs[L2TP_ATTR_RECV_TIMEOUT]);
  473. if (info->attrs[L2TP_ATTR_MTU])
  474. cfg.mtu = nla_get_u16(info->attrs[L2TP_ATTR_MTU]);
  475. if (info->attrs[L2TP_ATTR_MRU])
  476. cfg.mru = nla_get_u16(info->attrs[L2TP_ATTR_MRU]);
  477. if ((l2tp_nl_cmd_ops[cfg.pw_type] == NULL) ||
  478. (l2tp_nl_cmd_ops[cfg.pw_type]->session_create == NULL)) {
  479. ret = -EPROTONOSUPPORT;
  480. goto out;
  481. }
  482. /* Check that pseudowire-specific params are present */
  483. switch (cfg.pw_type) {
  484. case L2TP_PWTYPE_NONE:
  485. break;
  486. case L2TP_PWTYPE_ETH_VLAN:
  487. if (!info->attrs[L2TP_ATTR_VLAN_ID]) {
  488. ret = -EINVAL;
  489. goto out;
  490. }
  491. break;
  492. case L2TP_PWTYPE_ETH:
  493. break;
  494. case L2TP_PWTYPE_PPP:
  495. case L2TP_PWTYPE_PPP_AC:
  496. break;
  497. case L2TP_PWTYPE_IP:
  498. default:
  499. ret = -EPROTONOSUPPORT;
  500. break;
  501. }
  502. ret = -EPROTONOSUPPORT;
  503. if (l2tp_nl_cmd_ops[cfg.pw_type]->session_create)
  504. ret = (*l2tp_nl_cmd_ops[cfg.pw_type]->session_create)(net, tunnel_id,
  505. session_id, peer_session_id, &cfg);
  506. if (ret >= 0) {
  507. session = l2tp_session_find(net, tunnel, session_id);
  508. if (session)
  509. ret = l2tp_session_notify(&l2tp_nl_family, info, session,
  510. L2TP_CMD_SESSION_CREATE);
  511. }
  512. out:
  513. return ret;
  514. }
  515. static int l2tp_nl_cmd_session_delete(struct sk_buff *skb, struct genl_info *info)
  516. {
  517. int ret = 0;
  518. struct l2tp_session *session;
  519. u16 pw_type;
  520. session = l2tp_nl_session_find(info);
  521. if (session == NULL) {
  522. ret = -ENODEV;
  523. goto out;
  524. }
  525. l2tp_session_notify(&l2tp_nl_family, info,
  526. session, L2TP_CMD_SESSION_DELETE);
  527. pw_type = session->pwtype;
  528. if (pw_type < __L2TP_PWTYPE_MAX)
  529. if (l2tp_nl_cmd_ops[pw_type] && l2tp_nl_cmd_ops[pw_type]->session_delete)
  530. ret = (*l2tp_nl_cmd_ops[pw_type]->session_delete)(session);
  531. out:
  532. return ret;
  533. }
  534. static int l2tp_nl_cmd_session_modify(struct sk_buff *skb, struct genl_info *info)
  535. {
  536. int ret = 0;
  537. struct l2tp_session *session;
  538. session = l2tp_nl_session_find(info);
  539. if (session == NULL) {
  540. ret = -ENODEV;
  541. goto out;
  542. }
  543. if (info->attrs[L2TP_ATTR_DEBUG])
  544. session->debug = nla_get_u32(info->attrs[L2TP_ATTR_DEBUG]);
  545. if (info->attrs[L2TP_ATTR_DATA_SEQ])
  546. session->data_seq = nla_get_u8(info->attrs[L2TP_ATTR_DATA_SEQ]);
  547. if (info->attrs[L2TP_ATTR_RECV_SEQ])
  548. session->recv_seq = nla_get_u8(info->attrs[L2TP_ATTR_RECV_SEQ]);
  549. if (info->attrs[L2TP_ATTR_SEND_SEQ]) {
  550. session->send_seq = nla_get_u8(info->attrs[L2TP_ATTR_SEND_SEQ]);
  551. l2tp_session_set_header_len(session, session->tunnel->version);
  552. }
  553. if (info->attrs[L2TP_ATTR_LNS_MODE])
  554. session->lns_mode = nla_get_u8(info->attrs[L2TP_ATTR_LNS_MODE]);
  555. if (info->attrs[L2TP_ATTR_RECV_TIMEOUT])
  556. session->reorder_timeout = nla_get_msecs(info->attrs[L2TP_ATTR_RECV_TIMEOUT]);
  557. if (info->attrs[L2TP_ATTR_MTU])
  558. session->mtu = nla_get_u16(info->attrs[L2TP_ATTR_MTU]);
  559. if (info->attrs[L2TP_ATTR_MRU])
  560. session->mru = nla_get_u16(info->attrs[L2TP_ATTR_MRU]);
  561. ret = l2tp_session_notify(&l2tp_nl_family, info,
  562. session, L2TP_CMD_SESSION_MODIFY);
  563. out:
  564. return ret;
  565. }
  566. static int l2tp_nl_session_send(struct sk_buff *skb, u32 portid, u32 seq, int flags,
  567. struct l2tp_session *session, u8 cmd)
  568. {
  569. void *hdr;
  570. struct nlattr *nest;
  571. struct l2tp_tunnel *tunnel = session->tunnel;
  572. struct sock *sk = NULL;
  573. sk = tunnel->sock;
  574. hdr = genlmsg_put(skb, portid, seq, &l2tp_nl_family, flags, cmd);
  575. if (!hdr)
  576. return -EMSGSIZE;
  577. if (nla_put_u32(skb, L2TP_ATTR_CONN_ID, tunnel->tunnel_id) ||
  578. nla_put_u32(skb, L2TP_ATTR_SESSION_ID, session->session_id) ||
  579. nla_put_u32(skb, L2TP_ATTR_PEER_CONN_ID, tunnel->peer_tunnel_id) ||
  580. nla_put_u32(skb, L2TP_ATTR_PEER_SESSION_ID,
  581. session->peer_session_id) ||
  582. nla_put_u32(skb, L2TP_ATTR_DEBUG, session->debug) ||
  583. nla_put_u16(skb, L2TP_ATTR_PW_TYPE, session->pwtype) ||
  584. nla_put_u16(skb, L2TP_ATTR_MTU, session->mtu) ||
  585. (session->mru &&
  586. nla_put_u16(skb, L2TP_ATTR_MRU, session->mru)))
  587. goto nla_put_failure;
  588. if ((session->ifname[0] &&
  589. nla_put_string(skb, L2TP_ATTR_IFNAME, session->ifname)) ||
  590. (session->cookie_len &&
  591. nla_put(skb, L2TP_ATTR_COOKIE, session->cookie_len,
  592. &session->cookie[0])) ||
  593. (session->peer_cookie_len &&
  594. nla_put(skb, L2TP_ATTR_PEER_COOKIE, session->peer_cookie_len,
  595. &session->peer_cookie[0])) ||
  596. nla_put_u8(skb, L2TP_ATTR_RECV_SEQ, session->recv_seq) ||
  597. nla_put_u8(skb, L2TP_ATTR_SEND_SEQ, session->send_seq) ||
  598. nla_put_u8(skb, L2TP_ATTR_LNS_MODE, session->lns_mode) ||
  599. #ifdef CONFIG_XFRM
  600. (((sk) && (sk->sk_policy[0] || sk->sk_policy[1])) &&
  601. nla_put_u8(skb, L2TP_ATTR_USING_IPSEC, 1)) ||
  602. #endif
  603. (session->reorder_timeout &&
  604. nla_put_msecs(skb, L2TP_ATTR_RECV_TIMEOUT, session->reorder_timeout)))
  605. goto nla_put_failure;
  606. nest = nla_nest_start(skb, L2TP_ATTR_STATS);
  607. if (nest == NULL)
  608. goto nla_put_failure;
  609. if (nla_put_u64(skb, L2TP_ATTR_TX_PACKETS,
  610. atomic_long_read(&session->stats.tx_packets)) ||
  611. nla_put_u64(skb, L2TP_ATTR_TX_BYTES,
  612. atomic_long_read(&session->stats.tx_bytes)) ||
  613. nla_put_u64(skb, L2TP_ATTR_TX_ERRORS,
  614. atomic_long_read(&session->stats.tx_errors)) ||
  615. nla_put_u64(skb, L2TP_ATTR_RX_PACKETS,
  616. atomic_long_read(&session->stats.rx_packets)) ||
  617. nla_put_u64(skb, L2TP_ATTR_RX_BYTES,
  618. atomic_long_read(&session->stats.rx_bytes)) ||
  619. nla_put_u64(skb, L2TP_ATTR_RX_SEQ_DISCARDS,
  620. atomic_long_read(&session->stats.rx_seq_discards)) ||
  621. nla_put_u64(skb, L2TP_ATTR_RX_OOS_PACKETS,
  622. atomic_long_read(&session->stats.rx_oos_packets)) ||
  623. nla_put_u64(skb, L2TP_ATTR_RX_ERRORS,
  624. atomic_long_read(&session->stats.rx_errors)))
  625. goto nla_put_failure;
  626. nla_nest_end(skb, nest);
  627. return genlmsg_end(skb, hdr);
  628. nla_put_failure:
  629. genlmsg_cancel(skb, hdr);
  630. return -1;
  631. }
  632. static int l2tp_nl_cmd_session_get(struct sk_buff *skb, struct genl_info *info)
  633. {
  634. struct l2tp_session *session;
  635. struct sk_buff *msg;
  636. int ret;
  637. session = l2tp_nl_session_find(info);
  638. if (session == NULL) {
  639. ret = -ENODEV;
  640. goto out;
  641. }
  642. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  643. if (!msg) {
  644. ret = -ENOMEM;
  645. goto out;
  646. }
  647. ret = l2tp_nl_session_send(msg, info->snd_portid, info->snd_seq,
  648. 0, session, L2TP_CMD_SESSION_GET);
  649. if (ret < 0)
  650. goto err_out;
  651. return genlmsg_unicast(genl_info_net(info), msg, info->snd_portid);
  652. err_out:
  653. nlmsg_free(msg);
  654. out:
  655. return ret;
  656. }
  657. static int l2tp_nl_cmd_session_dump(struct sk_buff *skb, struct netlink_callback *cb)
  658. {
  659. struct net *net = sock_net(skb->sk);
  660. struct l2tp_session *session;
  661. struct l2tp_tunnel *tunnel = NULL;
  662. int ti = cb->args[0];
  663. int si = cb->args[1];
  664. for (;;) {
  665. if (tunnel == NULL) {
  666. tunnel = l2tp_tunnel_find_nth(net, ti);
  667. if (tunnel == NULL)
  668. goto out;
  669. }
  670. session = l2tp_session_find_nth(tunnel, si);
  671. if (session == NULL) {
  672. ti++;
  673. tunnel = NULL;
  674. si = 0;
  675. continue;
  676. }
  677. if (l2tp_nl_session_send(skb, NETLINK_CB(cb->skb).portid,
  678. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  679. session, L2TP_CMD_SESSION_GET) <= 0)
  680. break;
  681. si++;
  682. }
  683. out:
  684. cb->args[0] = ti;
  685. cb->args[1] = si;
  686. return skb->len;
  687. }
  688. static struct nla_policy l2tp_nl_policy[L2TP_ATTR_MAX + 1] = {
  689. [L2TP_ATTR_NONE] = { .type = NLA_UNSPEC, },
  690. [L2TP_ATTR_PW_TYPE] = { .type = NLA_U16, },
  691. [L2TP_ATTR_ENCAP_TYPE] = { .type = NLA_U16, },
  692. [L2TP_ATTR_OFFSET] = { .type = NLA_U16, },
  693. [L2TP_ATTR_DATA_SEQ] = { .type = NLA_U8, },
  694. [L2TP_ATTR_L2SPEC_TYPE] = { .type = NLA_U8, },
  695. [L2TP_ATTR_L2SPEC_LEN] = { .type = NLA_U8, },
  696. [L2TP_ATTR_PROTO_VERSION] = { .type = NLA_U8, },
  697. [L2TP_ATTR_CONN_ID] = { .type = NLA_U32, },
  698. [L2TP_ATTR_PEER_CONN_ID] = { .type = NLA_U32, },
  699. [L2TP_ATTR_SESSION_ID] = { .type = NLA_U32, },
  700. [L2TP_ATTR_PEER_SESSION_ID] = { .type = NLA_U32, },
  701. [L2TP_ATTR_UDP_CSUM] = { .type = NLA_U8, },
  702. [L2TP_ATTR_VLAN_ID] = { .type = NLA_U16, },
  703. [L2TP_ATTR_DEBUG] = { .type = NLA_U32, },
  704. [L2TP_ATTR_RECV_SEQ] = { .type = NLA_U8, },
  705. [L2TP_ATTR_SEND_SEQ] = { .type = NLA_U8, },
  706. [L2TP_ATTR_LNS_MODE] = { .type = NLA_U8, },
  707. [L2TP_ATTR_USING_IPSEC] = { .type = NLA_U8, },
  708. [L2TP_ATTR_RECV_TIMEOUT] = { .type = NLA_MSECS, },
  709. [L2TP_ATTR_FD] = { .type = NLA_U32, },
  710. [L2TP_ATTR_IP_SADDR] = { .type = NLA_U32, },
  711. [L2TP_ATTR_IP_DADDR] = { .type = NLA_U32, },
  712. [L2TP_ATTR_UDP_SPORT] = { .type = NLA_U16, },
  713. [L2TP_ATTR_UDP_DPORT] = { .type = NLA_U16, },
  714. [L2TP_ATTR_MTU] = { .type = NLA_U16, },
  715. [L2TP_ATTR_MRU] = { .type = NLA_U16, },
  716. [L2TP_ATTR_STATS] = { .type = NLA_NESTED, },
  717. [L2TP_ATTR_IP6_SADDR] = {
  718. .type = NLA_BINARY,
  719. .len = sizeof(struct in6_addr),
  720. },
  721. [L2TP_ATTR_IP6_DADDR] = {
  722. .type = NLA_BINARY,
  723. .len = sizeof(struct in6_addr),
  724. },
  725. [L2TP_ATTR_IFNAME] = {
  726. .type = NLA_NUL_STRING,
  727. .len = IFNAMSIZ - 1,
  728. },
  729. [L2TP_ATTR_COOKIE] = {
  730. .type = NLA_BINARY,
  731. .len = 8,
  732. },
  733. [L2TP_ATTR_PEER_COOKIE] = {
  734. .type = NLA_BINARY,
  735. .len = 8,
  736. },
  737. };
  738. static const struct genl_ops l2tp_nl_ops[] = {
  739. {
  740. .cmd = L2TP_CMD_NOOP,
  741. .doit = l2tp_nl_cmd_noop,
  742. .policy = l2tp_nl_policy,
  743. /* can be retrieved by unprivileged users */
  744. },
  745. {
  746. .cmd = L2TP_CMD_TUNNEL_CREATE,
  747. .doit = l2tp_nl_cmd_tunnel_create,
  748. .policy = l2tp_nl_policy,
  749. .flags = GENL_ADMIN_PERM,
  750. },
  751. {
  752. .cmd = L2TP_CMD_TUNNEL_DELETE,
  753. .doit = l2tp_nl_cmd_tunnel_delete,
  754. .policy = l2tp_nl_policy,
  755. .flags = GENL_ADMIN_PERM,
  756. },
  757. {
  758. .cmd = L2TP_CMD_TUNNEL_MODIFY,
  759. .doit = l2tp_nl_cmd_tunnel_modify,
  760. .policy = l2tp_nl_policy,
  761. .flags = GENL_ADMIN_PERM,
  762. },
  763. {
  764. .cmd = L2TP_CMD_TUNNEL_GET,
  765. .doit = l2tp_nl_cmd_tunnel_get,
  766. .dumpit = l2tp_nl_cmd_tunnel_dump,
  767. .policy = l2tp_nl_policy,
  768. .flags = GENL_ADMIN_PERM,
  769. },
  770. {
  771. .cmd = L2TP_CMD_SESSION_CREATE,
  772. .doit = l2tp_nl_cmd_session_create,
  773. .policy = l2tp_nl_policy,
  774. .flags = GENL_ADMIN_PERM,
  775. },
  776. {
  777. .cmd = L2TP_CMD_SESSION_DELETE,
  778. .doit = l2tp_nl_cmd_session_delete,
  779. .policy = l2tp_nl_policy,
  780. .flags = GENL_ADMIN_PERM,
  781. },
  782. {
  783. .cmd = L2TP_CMD_SESSION_MODIFY,
  784. .doit = l2tp_nl_cmd_session_modify,
  785. .policy = l2tp_nl_policy,
  786. .flags = GENL_ADMIN_PERM,
  787. },
  788. {
  789. .cmd = L2TP_CMD_SESSION_GET,
  790. .doit = l2tp_nl_cmd_session_get,
  791. .dumpit = l2tp_nl_cmd_session_dump,
  792. .policy = l2tp_nl_policy,
  793. .flags = GENL_ADMIN_PERM,
  794. },
  795. };
  796. int l2tp_nl_register_ops(enum l2tp_pwtype pw_type, const struct l2tp_nl_cmd_ops *ops)
  797. {
  798. int ret;
  799. ret = -EINVAL;
  800. if (pw_type >= __L2TP_PWTYPE_MAX)
  801. goto err;
  802. genl_lock();
  803. ret = -EBUSY;
  804. if (l2tp_nl_cmd_ops[pw_type])
  805. goto out;
  806. l2tp_nl_cmd_ops[pw_type] = ops;
  807. ret = 0;
  808. out:
  809. genl_unlock();
  810. err:
  811. return ret;
  812. }
  813. EXPORT_SYMBOL_GPL(l2tp_nl_register_ops);
  814. void l2tp_nl_unregister_ops(enum l2tp_pwtype pw_type)
  815. {
  816. if (pw_type < __L2TP_PWTYPE_MAX) {
  817. genl_lock();
  818. l2tp_nl_cmd_ops[pw_type] = NULL;
  819. genl_unlock();
  820. }
  821. }
  822. EXPORT_SYMBOL_GPL(l2tp_nl_unregister_ops);
  823. static int l2tp_nl_init(void)
  824. {
  825. pr_info("L2TP netlink interface\n");
  826. return genl_register_family_with_ops_groups(&l2tp_nl_family,
  827. l2tp_nl_ops,
  828. l2tp_multicast_group);
  829. }
  830. static void l2tp_nl_cleanup(void)
  831. {
  832. genl_unregister_family(&l2tp_nl_family);
  833. }
  834. module_init(l2tp_nl_init);
  835. module_exit(l2tp_nl_cleanup);
  836. MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
  837. MODULE_DESCRIPTION("L2TP netlink");
  838. MODULE_LICENSE("GPL");
  839. MODULE_VERSION("1.0");
  840. MODULE_ALIAS_GENL_FAMILY("l2tp");