l2tp_netlink.c 26 KB

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