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