l2tp_netlink.c 27 KB

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