l2tp_netlink.c 27 KB

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