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