l2tp_netlink.c 25 KB

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