l2tp_netlink.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885
  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. #include <net/sock.h>
  17. #include <net/genetlink.h>
  18. #include <net/udp.h>
  19. #include <linux/in.h>
  20. #include <linux/udp.h>
  21. #include <linux/socket.h>
  22. #include <linux/module.h>
  23. #include <linux/list.h>
  24. #include <net/net_namespace.h>
  25. #include <linux/l2tp.h>
  26. #include "l2tp_core.h"
  27. static struct genl_family l2tp_nl_family = {
  28. .id = GENL_ID_GENERATE,
  29. .name = L2TP_GENL_NAME,
  30. .version = L2TP_GENL_VERSION,
  31. .hdrsize = 0,
  32. .maxattr = L2TP_ATTR_MAX,
  33. };
  34. /* Accessed under genl lock */
  35. static const struct l2tp_nl_cmd_ops *l2tp_nl_cmd_ops[__L2TP_PWTYPE_MAX];
  36. static struct l2tp_session *l2tp_nl_session_find(struct genl_info *info)
  37. {
  38. u32 tunnel_id;
  39. u32 session_id;
  40. char *ifname;
  41. struct l2tp_tunnel *tunnel;
  42. struct l2tp_session *session = NULL;
  43. struct net *net = genl_info_net(info);
  44. if (info->attrs[L2TP_ATTR_IFNAME]) {
  45. ifname = nla_data(info->attrs[L2TP_ATTR_IFNAME]);
  46. session = l2tp_session_find_by_ifname(net, ifname);
  47. } else if ((info->attrs[L2TP_ATTR_SESSION_ID]) &&
  48. (info->attrs[L2TP_ATTR_CONN_ID])) {
  49. tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
  50. session_id = nla_get_u32(info->attrs[L2TP_ATTR_SESSION_ID]);
  51. tunnel = l2tp_tunnel_find(net, tunnel_id);
  52. if (tunnel)
  53. session = l2tp_session_find(net, tunnel, session_id);
  54. }
  55. return session;
  56. }
  57. static int l2tp_nl_cmd_noop(struct sk_buff *skb, struct genl_info *info)
  58. {
  59. struct sk_buff *msg;
  60. void *hdr;
  61. int ret = -ENOBUFS;
  62. msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  63. if (!msg) {
  64. ret = -ENOMEM;
  65. goto out;
  66. }
  67. hdr = genlmsg_put(msg, info->snd_pid, info->snd_seq,
  68. &l2tp_nl_family, 0, L2TP_CMD_NOOP);
  69. if (IS_ERR(hdr)) {
  70. ret = PTR_ERR(hdr);
  71. goto err_out;
  72. }
  73. genlmsg_end(msg, hdr);
  74. return genlmsg_unicast(genl_info_net(info), msg, info->snd_pid);
  75. err_out:
  76. nlmsg_free(msg);
  77. out:
  78. return ret;
  79. }
  80. static int l2tp_nl_cmd_tunnel_create(struct sk_buff *skb, struct genl_info *info)
  81. {
  82. u32 tunnel_id;
  83. u32 peer_tunnel_id;
  84. int proto_version;
  85. int fd;
  86. int ret = 0;
  87. struct l2tp_tunnel_cfg cfg = { 0, };
  88. struct l2tp_tunnel *tunnel;
  89. struct net *net = genl_info_net(info);
  90. if (!info->attrs[L2TP_ATTR_CONN_ID]) {
  91. ret = -EINVAL;
  92. goto out;
  93. }
  94. tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
  95. if (!info->attrs[L2TP_ATTR_PEER_CONN_ID]) {
  96. ret = -EINVAL;
  97. goto out;
  98. }
  99. peer_tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_PEER_CONN_ID]);
  100. if (!info->attrs[L2TP_ATTR_PROTO_VERSION]) {
  101. ret = -EINVAL;
  102. goto out;
  103. }
  104. proto_version = nla_get_u8(info->attrs[L2TP_ATTR_PROTO_VERSION]);
  105. if (!info->attrs[L2TP_ATTR_ENCAP_TYPE]) {
  106. ret = -EINVAL;
  107. goto out;
  108. }
  109. cfg.encap = nla_get_u16(info->attrs[L2TP_ATTR_ENCAP_TYPE]);
  110. fd = -1;
  111. if (info->attrs[L2TP_ATTR_FD]) {
  112. fd = nla_get_u32(info->attrs[L2TP_ATTR_FD]);
  113. } else {
  114. if (info->attrs[L2TP_ATTR_IP_SADDR])
  115. cfg.local_ip.s_addr = nla_get_be32(info->attrs[L2TP_ATTR_IP_SADDR]);
  116. if (info->attrs[L2TP_ATTR_IP_DADDR])
  117. cfg.peer_ip.s_addr = nla_get_be32(info->attrs[L2TP_ATTR_IP_DADDR]);
  118. if (info->attrs[L2TP_ATTR_UDP_SPORT])
  119. cfg.local_udp_port = nla_get_u16(info->attrs[L2TP_ATTR_UDP_SPORT]);
  120. if (info->attrs[L2TP_ATTR_UDP_DPORT])
  121. cfg.peer_udp_port = nla_get_u16(info->attrs[L2TP_ATTR_UDP_DPORT]);
  122. if (info->attrs[L2TP_ATTR_UDP_CSUM])
  123. cfg.use_udp_checksums = nla_get_flag(info->attrs[L2TP_ATTR_UDP_CSUM]);
  124. }
  125. if (info->attrs[L2TP_ATTR_DEBUG])
  126. cfg.debug = nla_get_u32(info->attrs[L2TP_ATTR_DEBUG]);
  127. tunnel = l2tp_tunnel_find(net, tunnel_id);
  128. if (tunnel != NULL) {
  129. ret = -EEXIST;
  130. goto out;
  131. }
  132. ret = -EINVAL;
  133. switch (cfg.encap) {
  134. case L2TP_ENCAPTYPE_UDP:
  135. case L2TP_ENCAPTYPE_IP:
  136. ret = l2tp_tunnel_create(net, fd, proto_version, tunnel_id,
  137. peer_tunnel_id, &cfg, &tunnel);
  138. break;
  139. }
  140. out:
  141. return ret;
  142. }
  143. static int l2tp_nl_cmd_tunnel_delete(struct sk_buff *skb, struct genl_info *info)
  144. {
  145. struct l2tp_tunnel *tunnel;
  146. u32 tunnel_id;
  147. int ret = 0;
  148. struct net *net = genl_info_net(info);
  149. if (!info->attrs[L2TP_ATTR_CONN_ID]) {
  150. ret = -EINVAL;
  151. goto out;
  152. }
  153. tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
  154. tunnel = l2tp_tunnel_find(net, tunnel_id);
  155. if (tunnel == NULL) {
  156. ret = -ENODEV;
  157. goto out;
  158. }
  159. (void) l2tp_tunnel_delete(tunnel);
  160. out:
  161. return ret;
  162. }
  163. static int l2tp_nl_cmd_tunnel_modify(struct sk_buff *skb, struct genl_info *info)
  164. {
  165. struct l2tp_tunnel *tunnel;
  166. u32 tunnel_id;
  167. int ret = 0;
  168. struct net *net = genl_info_net(info);
  169. if (!info->attrs[L2TP_ATTR_CONN_ID]) {
  170. ret = -EINVAL;
  171. goto out;
  172. }
  173. tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
  174. tunnel = l2tp_tunnel_find(net, tunnel_id);
  175. if (tunnel == NULL) {
  176. ret = -ENODEV;
  177. goto out;
  178. }
  179. if (info->attrs[L2TP_ATTR_DEBUG])
  180. tunnel->debug = nla_get_u32(info->attrs[L2TP_ATTR_DEBUG]);
  181. out:
  182. return ret;
  183. }
  184. static int l2tp_nl_tunnel_send(struct sk_buff *skb, u32 pid, u32 seq, int flags,
  185. struct l2tp_tunnel *tunnel)
  186. {
  187. void *hdr;
  188. struct nlattr *nest;
  189. struct sock *sk = NULL;
  190. struct inet_sock *inet;
  191. struct l2tp_stats stats;
  192. unsigned int start;
  193. hdr = genlmsg_put(skb, pid, seq, &l2tp_nl_family, flags,
  194. L2TP_CMD_TUNNEL_GET);
  195. if (IS_ERR(hdr))
  196. return PTR_ERR(hdr);
  197. if (nla_put_u8(skb, L2TP_ATTR_PROTO_VERSION, tunnel->version) ||
  198. nla_put_u32(skb, L2TP_ATTR_CONN_ID, tunnel->tunnel_id) ||
  199. nla_put_u32(skb, L2TP_ATTR_PEER_CONN_ID, tunnel->peer_tunnel_id) ||
  200. nla_put_u32(skb, L2TP_ATTR_DEBUG, tunnel->debug) ||
  201. nla_put_u16(skb, L2TP_ATTR_ENCAP_TYPE, tunnel->encap))
  202. goto nla_put_failure;
  203. nest = nla_nest_start(skb, L2TP_ATTR_STATS);
  204. if (nest == NULL)
  205. goto nla_put_failure;
  206. do {
  207. start = u64_stats_fetch_begin(&tunnel->stats.syncp);
  208. stats.tx_packets = tunnel->stats.tx_packets;
  209. stats.tx_bytes = tunnel->stats.tx_bytes;
  210. stats.tx_errors = tunnel->stats.tx_errors;
  211. stats.rx_packets = tunnel->stats.rx_packets;
  212. stats.rx_bytes = tunnel->stats.rx_bytes;
  213. stats.rx_errors = tunnel->stats.rx_errors;
  214. stats.rx_seq_discards = tunnel->stats.rx_seq_discards;
  215. stats.rx_oos_packets = tunnel->stats.rx_oos_packets;
  216. } while (u64_stats_fetch_retry(&tunnel->stats.syncp, start));
  217. if (nla_put_u64(skb, L2TP_ATTR_TX_PACKETS, stats.tx_packets) ||
  218. nla_put_u64(skb, L2TP_ATTR_TX_BYTES, stats.tx_bytes) ||
  219. nla_put_u64(skb, L2TP_ATTR_TX_ERRORS, stats.tx_errors) ||
  220. nla_put_u64(skb, L2TP_ATTR_RX_PACKETS, stats.rx_packets) ||
  221. nla_put_u64(skb, L2TP_ATTR_RX_BYTES, stats.rx_bytes) ||
  222. nla_put_u64(skb, L2TP_ATTR_RX_SEQ_DISCARDS,
  223. stats.rx_seq_discards) ||
  224. nla_put_u64(skb, L2TP_ATTR_RX_OOS_PACKETS,
  225. stats.rx_oos_packets) ||
  226. nla_put_u64(skb, L2TP_ATTR_RX_ERRORS, stats.rx_errors))
  227. goto nla_put_failure;
  228. nla_nest_end(skb, nest);
  229. sk = tunnel->sock;
  230. if (!sk)
  231. goto out;
  232. inet = inet_sk(sk);
  233. switch (tunnel->encap) {
  234. case L2TP_ENCAPTYPE_UDP:
  235. if (nla_put_u16(skb, L2TP_ATTR_UDP_SPORT, ntohs(inet->inet_sport)) ||
  236. nla_put_u16(skb, L2TP_ATTR_UDP_DPORT, ntohs(inet->inet_dport)) ||
  237. nla_put_u8(skb, L2TP_ATTR_UDP_CSUM,
  238. (sk->sk_no_check != UDP_CSUM_NOXMIT)))
  239. goto nla_put_failure;
  240. /* NOBREAK */
  241. case L2TP_ENCAPTYPE_IP:
  242. if (nla_put_be32(skb, L2TP_ATTR_IP_SADDR, inet->inet_saddr) ||
  243. nla_put_be32(skb, L2TP_ATTR_IP_DADDR, inet->inet_daddr))
  244. goto nla_put_failure;
  245. break;
  246. }
  247. out:
  248. return genlmsg_end(skb, hdr);
  249. nla_put_failure:
  250. genlmsg_cancel(skb, hdr);
  251. return -1;
  252. }
  253. static int l2tp_nl_cmd_tunnel_get(struct sk_buff *skb, struct genl_info *info)
  254. {
  255. struct l2tp_tunnel *tunnel;
  256. struct sk_buff *msg;
  257. u32 tunnel_id;
  258. int ret = -ENOBUFS;
  259. struct net *net = genl_info_net(info);
  260. if (!info->attrs[L2TP_ATTR_CONN_ID]) {
  261. ret = -EINVAL;
  262. goto out;
  263. }
  264. tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
  265. tunnel = l2tp_tunnel_find(net, tunnel_id);
  266. if (tunnel == NULL) {
  267. ret = -ENODEV;
  268. goto out;
  269. }
  270. msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  271. if (!msg) {
  272. ret = -ENOMEM;
  273. goto out;
  274. }
  275. ret = l2tp_nl_tunnel_send(msg, info->snd_pid, info->snd_seq,
  276. NLM_F_ACK, tunnel);
  277. if (ret < 0)
  278. goto err_out;
  279. return genlmsg_unicast(net, msg, info->snd_pid);
  280. err_out:
  281. nlmsg_free(msg);
  282. out:
  283. return ret;
  284. }
  285. static int l2tp_nl_cmd_tunnel_dump(struct sk_buff *skb, struct netlink_callback *cb)
  286. {
  287. int ti = cb->args[0];
  288. struct l2tp_tunnel *tunnel;
  289. struct net *net = sock_net(skb->sk);
  290. for (;;) {
  291. tunnel = l2tp_tunnel_find_nth(net, ti);
  292. if (tunnel == NULL)
  293. goto out;
  294. if (l2tp_nl_tunnel_send(skb, NETLINK_CB(cb->skb).pid,
  295. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  296. tunnel) <= 0)
  297. goto out;
  298. ti++;
  299. }
  300. out:
  301. cb->args[0] = ti;
  302. return skb->len;
  303. }
  304. static int l2tp_nl_cmd_session_create(struct sk_buff *skb, struct genl_info *info)
  305. {
  306. u32 tunnel_id = 0;
  307. u32 session_id;
  308. u32 peer_session_id;
  309. int ret = 0;
  310. struct l2tp_tunnel *tunnel;
  311. struct l2tp_session *session;
  312. struct l2tp_session_cfg cfg = { 0, };
  313. struct net *net = genl_info_net(info);
  314. if (!info->attrs[L2TP_ATTR_CONN_ID]) {
  315. ret = -EINVAL;
  316. goto out;
  317. }
  318. tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
  319. tunnel = l2tp_tunnel_find(net, tunnel_id);
  320. if (!tunnel) {
  321. ret = -ENODEV;
  322. goto out;
  323. }
  324. if (!info->attrs[L2TP_ATTR_SESSION_ID]) {
  325. ret = -EINVAL;
  326. goto out;
  327. }
  328. session_id = nla_get_u32(info->attrs[L2TP_ATTR_SESSION_ID]);
  329. session = l2tp_session_find(net, tunnel, session_id);
  330. if (session) {
  331. ret = -EEXIST;
  332. goto out;
  333. }
  334. if (!info->attrs[L2TP_ATTR_PEER_SESSION_ID]) {
  335. ret = -EINVAL;
  336. goto out;
  337. }
  338. peer_session_id = nla_get_u32(info->attrs[L2TP_ATTR_PEER_SESSION_ID]);
  339. if (!info->attrs[L2TP_ATTR_PW_TYPE]) {
  340. ret = -EINVAL;
  341. goto out;
  342. }
  343. cfg.pw_type = nla_get_u16(info->attrs[L2TP_ATTR_PW_TYPE]);
  344. if (cfg.pw_type >= __L2TP_PWTYPE_MAX) {
  345. ret = -EINVAL;
  346. goto out;
  347. }
  348. if (tunnel->version > 2) {
  349. if (info->attrs[L2TP_ATTR_OFFSET])
  350. cfg.offset = nla_get_u16(info->attrs[L2TP_ATTR_OFFSET]);
  351. if (info->attrs[L2TP_ATTR_DATA_SEQ])
  352. cfg.data_seq = nla_get_u8(info->attrs[L2TP_ATTR_DATA_SEQ]);
  353. cfg.l2specific_type = L2TP_L2SPECTYPE_DEFAULT;
  354. if (info->attrs[L2TP_ATTR_L2SPEC_TYPE])
  355. cfg.l2specific_type = nla_get_u8(info->attrs[L2TP_ATTR_L2SPEC_TYPE]);
  356. cfg.l2specific_len = 4;
  357. if (info->attrs[L2TP_ATTR_L2SPEC_LEN])
  358. cfg.l2specific_len = nla_get_u8(info->attrs[L2TP_ATTR_L2SPEC_LEN]);
  359. if (info->attrs[L2TP_ATTR_COOKIE]) {
  360. u16 len = nla_len(info->attrs[L2TP_ATTR_COOKIE]);
  361. if (len > 8) {
  362. ret = -EINVAL;
  363. goto out;
  364. }
  365. cfg.cookie_len = len;
  366. memcpy(&cfg.cookie[0], nla_data(info->attrs[L2TP_ATTR_COOKIE]), len);
  367. }
  368. if (info->attrs[L2TP_ATTR_PEER_COOKIE]) {
  369. u16 len = nla_len(info->attrs[L2TP_ATTR_PEER_COOKIE]);
  370. if (len > 8) {
  371. ret = -EINVAL;
  372. goto out;
  373. }
  374. cfg.peer_cookie_len = len;
  375. memcpy(&cfg.peer_cookie[0], nla_data(info->attrs[L2TP_ATTR_PEER_COOKIE]), len);
  376. }
  377. if (info->attrs[L2TP_ATTR_IFNAME])
  378. cfg.ifname = nla_data(info->attrs[L2TP_ATTR_IFNAME]);
  379. if (info->attrs[L2TP_ATTR_VLAN_ID])
  380. cfg.vlan_id = nla_get_u16(info->attrs[L2TP_ATTR_VLAN_ID]);
  381. }
  382. if (info->attrs[L2TP_ATTR_DEBUG])
  383. cfg.debug = nla_get_u32(info->attrs[L2TP_ATTR_DEBUG]);
  384. if (info->attrs[L2TP_ATTR_RECV_SEQ])
  385. cfg.recv_seq = nla_get_u8(info->attrs[L2TP_ATTR_RECV_SEQ]);
  386. if (info->attrs[L2TP_ATTR_SEND_SEQ])
  387. cfg.send_seq = nla_get_u8(info->attrs[L2TP_ATTR_SEND_SEQ]);
  388. if (info->attrs[L2TP_ATTR_LNS_MODE])
  389. cfg.lns_mode = nla_get_u8(info->attrs[L2TP_ATTR_LNS_MODE]);
  390. if (info->attrs[L2TP_ATTR_RECV_TIMEOUT])
  391. cfg.reorder_timeout = nla_get_msecs(info->attrs[L2TP_ATTR_RECV_TIMEOUT]);
  392. if (info->attrs[L2TP_ATTR_MTU])
  393. cfg.mtu = nla_get_u16(info->attrs[L2TP_ATTR_MTU]);
  394. if (info->attrs[L2TP_ATTR_MRU])
  395. cfg.mru = nla_get_u16(info->attrs[L2TP_ATTR_MRU]);
  396. if ((l2tp_nl_cmd_ops[cfg.pw_type] == NULL) ||
  397. (l2tp_nl_cmd_ops[cfg.pw_type]->session_create == NULL)) {
  398. ret = -EPROTONOSUPPORT;
  399. goto out;
  400. }
  401. /* Check that pseudowire-specific params are present */
  402. switch (cfg.pw_type) {
  403. case L2TP_PWTYPE_NONE:
  404. break;
  405. case L2TP_PWTYPE_ETH_VLAN:
  406. if (!info->attrs[L2TP_ATTR_VLAN_ID]) {
  407. ret = -EINVAL;
  408. goto out;
  409. }
  410. break;
  411. case L2TP_PWTYPE_ETH:
  412. break;
  413. case L2TP_PWTYPE_PPP:
  414. case L2TP_PWTYPE_PPP_AC:
  415. break;
  416. case L2TP_PWTYPE_IP:
  417. default:
  418. ret = -EPROTONOSUPPORT;
  419. break;
  420. }
  421. ret = -EPROTONOSUPPORT;
  422. if (l2tp_nl_cmd_ops[cfg.pw_type]->session_create)
  423. ret = (*l2tp_nl_cmd_ops[cfg.pw_type]->session_create)(net, tunnel_id,
  424. session_id, peer_session_id, &cfg);
  425. out:
  426. return ret;
  427. }
  428. static int l2tp_nl_cmd_session_delete(struct sk_buff *skb, struct genl_info *info)
  429. {
  430. int ret = 0;
  431. struct l2tp_session *session;
  432. u16 pw_type;
  433. session = l2tp_nl_session_find(info);
  434. if (session == NULL) {
  435. ret = -ENODEV;
  436. goto out;
  437. }
  438. pw_type = session->pwtype;
  439. if (pw_type < __L2TP_PWTYPE_MAX)
  440. if (l2tp_nl_cmd_ops[pw_type] && l2tp_nl_cmd_ops[pw_type]->session_delete)
  441. ret = (*l2tp_nl_cmd_ops[pw_type]->session_delete)(session);
  442. out:
  443. return ret;
  444. }
  445. static int l2tp_nl_cmd_session_modify(struct sk_buff *skb, struct genl_info *info)
  446. {
  447. int ret = 0;
  448. struct l2tp_session *session;
  449. session = l2tp_nl_session_find(info);
  450. if (session == NULL) {
  451. ret = -ENODEV;
  452. goto out;
  453. }
  454. if (info->attrs[L2TP_ATTR_DEBUG])
  455. session->debug = nla_get_u32(info->attrs[L2TP_ATTR_DEBUG]);
  456. if (info->attrs[L2TP_ATTR_DATA_SEQ])
  457. session->data_seq = nla_get_u8(info->attrs[L2TP_ATTR_DATA_SEQ]);
  458. if (info->attrs[L2TP_ATTR_RECV_SEQ])
  459. session->recv_seq = nla_get_u8(info->attrs[L2TP_ATTR_RECV_SEQ]);
  460. if (info->attrs[L2TP_ATTR_SEND_SEQ])
  461. session->send_seq = nla_get_u8(info->attrs[L2TP_ATTR_SEND_SEQ]);
  462. if (info->attrs[L2TP_ATTR_LNS_MODE])
  463. session->lns_mode = nla_get_u8(info->attrs[L2TP_ATTR_LNS_MODE]);
  464. if (info->attrs[L2TP_ATTR_RECV_TIMEOUT])
  465. session->reorder_timeout = nla_get_msecs(info->attrs[L2TP_ATTR_RECV_TIMEOUT]);
  466. if (info->attrs[L2TP_ATTR_MTU])
  467. session->mtu = nla_get_u16(info->attrs[L2TP_ATTR_MTU]);
  468. if (info->attrs[L2TP_ATTR_MRU])
  469. session->mru = nla_get_u16(info->attrs[L2TP_ATTR_MRU]);
  470. out:
  471. return ret;
  472. }
  473. static int l2tp_nl_session_send(struct sk_buff *skb, u32 pid, u32 seq, int flags,
  474. struct l2tp_session *session)
  475. {
  476. void *hdr;
  477. struct nlattr *nest;
  478. struct l2tp_tunnel *tunnel = session->tunnel;
  479. struct sock *sk = NULL;
  480. struct l2tp_stats stats;
  481. unsigned int start;
  482. sk = tunnel->sock;
  483. hdr = genlmsg_put(skb, pid, seq, &l2tp_nl_family, flags, L2TP_CMD_SESSION_GET);
  484. if (IS_ERR(hdr))
  485. return PTR_ERR(hdr);
  486. if (nla_put_u32(skb, L2TP_ATTR_CONN_ID, tunnel->tunnel_id) ||
  487. nla_put_u32(skb, L2TP_ATTR_SESSION_ID, session->session_id) ||
  488. nla_put_u32(skb, L2TP_ATTR_PEER_CONN_ID, tunnel->peer_tunnel_id) ||
  489. nla_put_u32(skb, L2TP_ATTR_PEER_SESSION_ID,
  490. session->peer_session_id) ||
  491. nla_put_u32(skb, L2TP_ATTR_DEBUG, session->debug) ||
  492. nla_put_u16(skb, L2TP_ATTR_PW_TYPE, session->pwtype) ||
  493. nla_put_u16(skb, L2TP_ATTR_MTU, session->mtu) ||
  494. (session->mru &&
  495. nla_put_u16(skb, L2TP_ATTR_MRU, session->mru)))
  496. goto nla_put_failure;
  497. if ((session->ifname && session->ifname[0] &&
  498. nla_put_string(skb, L2TP_ATTR_IFNAME, session->ifname)) ||
  499. (session->cookie_len &&
  500. nla_put(skb, L2TP_ATTR_COOKIE, session->cookie_len,
  501. &session->cookie[0])) ||
  502. (session->peer_cookie_len &&
  503. nla_put(skb, L2TP_ATTR_PEER_COOKIE, session->peer_cookie_len,
  504. &session->peer_cookie[0])) ||
  505. nla_put_u8(skb, L2TP_ATTR_RECV_SEQ, session->recv_seq) ||
  506. nla_put_u8(skb, L2TP_ATTR_SEND_SEQ, session->send_seq) ||
  507. nla_put_u8(skb, L2TP_ATTR_LNS_MODE, session->lns_mode) ||
  508. #ifdef CONFIG_XFRM
  509. (((sk) && (sk->sk_policy[0] || sk->sk_policy[1])) &&
  510. nla_put_u8(skb, L2TP_ATTR_USING_IPSEC, 1)) ||
  511. #endif
  512. (session->reorder_timeout &&
  513. nla_put_msecs(skb, L2TP_ATTR_RECV_TIMEOUT, session->reorder_timeout)))
  514. goto nla_put_failure;
  515. nest = nla_nest_start(skb, L2TP_ATTR_STATS);
  516. if (nest == NULL)
  517. goto nla_put_failure;
  518. do {
  519. start = u64_stats_fetch_begin(&session->stats.syncp);
  520. stats.tx_packets = session->stats.tx_packets;
  521. stats.tx_bytes = session->stats.tx_bytes;
  522. stats.tx_errors = session->stats.tx_errors;
  523. stats.rx_packets = session->stats.rx_packets;
  524. stats.rx_bytes = session->stats.rx_bytes;
  525. stats.rx_errors = session->stats.rx_errors;
  526. stats.rx_seq_discards = session->stats.rx_seq_discards;
  527. stats.rx_oos_packets = session->stats.rx_oos_packets;
  528. } while (u64_stats_fetch_retry(&session->stats.syncp, start));
  529. if (nla_put_u64(skb, L2TP_ATTR_TX_PACKETS, stats.tx_packets) ||
  530. nla_put_u64(skb, L2TP_ATTR_TX_BYTES, stats.tx_bytes) ||
  531. nla_put_u64(skb, L2TP_ATTR_TX_ERRORS, stats.tx_errors) ||
  532. nla_put_u64(skb, L2TP_ATTR_RX_PACKETS, stats.rx_packets) ||
  533. nla_put_u64(skb, L2TP_ATTR_RX_BYTES, stats.rx_bytes) ||
  534. nla_put_u64(skb, L2TP_ATTR_RX_SEQ_DISCARDS,
  535. stats.rx_seq_discards) ||
  536. nla_put_u64(skb, L2TP_ATTR_RX_OOS_PACKETS,
  537. stats.rx_oos_packets) ||
  538. nla_put_u64(skb, L2TP_ATTR_RX_ERRORS, stats.rx_errors))
  539. goto nla_put_failure;
  540. nla_nest_end(skb, nest);
  541. return genlmsg_end(skb, hdr);
  542. nla_put_failure:
  543. genlmsg_cancel(skb, hdr);
  544. return -1;
  545. }
  546. static int l2tp_nl_cmd_session_get(struct sk_buff *skb, struct genl_info *info)
  547. {
  548. struct l2tp_session *session;
  549. struct sk_buff *msg;
  550. int ret;
  551. session = l2tp_nl_session_find(info);
  552. if (session == NULL) {
  553. ret = -ENODEV;
  554. goto out;
  555. }
  556. msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  557. if (!msg) {
  558. ret = -ENOMEM;
  559. goto out;
  560. }
  561. ret = l2tp_nl_session_send(msg, info->snd_pid, info->snd_seq,
  562. 0, session);
  563. if (ret < 0)
  564. goto err_out;
  565. return genlmsg_unicast(genl_info_net(info), msg, info->snd_pid);
  566. err_out:
  567. nlmsg_free(msg);
  568. out:
  569. return ret;
  570. }
  571. static int l2tp_nl_cmd_session_dump(struct sk_buff *skb, struct netlink_callback *cb)
  572. {
  573. struct net *net = sock_net(skb->sk);
  574. struct l2tp_session *session;
  575. struct l2tp_tunnel *tunnel = NULL;
  576. int ti = cb->args[0];
  577. int si = cb->args[1];
  578. for (;;) {
  579. if (tunnel == NULL) {
  580. tunnel = l2tp_tunnel_find_nth(net, ti);
  581. if (tunnel == NULL)
  582. goto out;
  583. }
  584. session = l2tp_session_find_nth(tunnel, si);
  585. if (session == NULL) {
  586. ti++;
  587. tunnel = NULL;
  588. si = 0;
  589. continue;
  590. }
  591. if (l2tp_nl_session_send(skb, NETLINK_CB(cb->skb).pid,
  592. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  593. session) <= 0)
  594. break;
  595. si++;
  596. }
  597. out:
  598. cb->args[0] = ti;
  599. cb->args[1] = si;
  600. return skb->len;
  601. }
  602. static struct nla_policy l2tp_nl_policy[L2TP_ATTR_MAX + 1] = {
  603. [L2TP_ATTR_NONE] = { .type = NLA_UNSPEC, },
  604. [L2TP_ATTR_PW_TYPE] = { .type = NLA_U16, },
  605. [L2TP_ATTR_ENCAP_TYPE] = { .type = NLA_U16, },
  606. [L2TP_ATTR_OFFSET] = { .type = NLA_U16, },
  607. [L2TP_ATTR_DATA_SEQ] = { .type = NLA_U8, },
  608. [L2TP_ATTR_L2SPEC_TYPE] = { .type = NLA_U8, },
  609. [L2TP_ATTR_L2SPEC_LEN] = { .type = NLA_U8, },
  610. [L2TP_ATTR_PROTO_VERSION] = { .type = NLA_U8, },
  611. [L2TP_ATTR_CONN_ID] = { .type = NLA_U32, },
  612. [L2TP_ATTR_PEER_CONN_ID] = { .type = NLA_U32, },
  613. [L2TP_ATTR_SESSION_ID] = { .type = NLA_U32, },
  614. [L2TP_ATTR_PEER_SESSION_ID] = { .type = NLA_U32, },
  615. [L2TP_ATTR_UDP_CSUM] = { .type = NLA_U8, },
  616. [L2TP_ATTR_VLAN_ID] = { .type = NLA_U16, },
  617. [L2TP_ATTR_DEBUG] = { .type = NLA_U32, },
  618. [L2TP_ATTR_RECV_SEQ] = { .type = NLA_U8, },
  619. [L2TP_ATTR_SEND_SEQ] = { .type = NLA_U8, },
  620. [L2TP_ATTR_LNS_MODE] = { .type = NLA_U8, },
  621. [L2TP_ATTR_USING_IPSEC] = { .type = NLA_U8, },
  622. [L2TP_ATTR_RECV_TIMEOUT] = { .type = NLA_MSECS, },
  623. [L2TP_ATTR_FD] = { .type = NLA_U32, },
  624. [L2TP_ATTR_IP_SADDR] = { .type = NLA_U32, },
  625. [L2TP_ATTR_IP_DADDR] = { .type = NLA_U32, },
  626. [L2TP_ATTR_UDP_SPORT] = { .type = NLA_U16, },
  627. [L2TP_ATTR_UDP_DPORT] = { .type = NLA_U16, },
  628. [L2TP_ATTR_MTU] = { .type = NLA_U16, },
  629. [L2TP_ATTR_MRU] = { .type = NLA_U16, },
  630. [L2TP_ATTR_STATS] = { .type = NLA_NESTED, },
  631. [L2TP_ATTR_IFNAME] = {
  632. .type = NLA_NUL_STRING,
  633. .len = IFNAMSIZ - 1,
  634. },
  635. [L2TP_ATTR_COOKIE] = {
  636. .type = NLA_BINARY,
  637. .len = 8,
  638. },
  639. [L2TP_ATTR_PEER_COOKIE] = {
  640. .type = NLA_BINARY,
  641. .len = 8,
  642. },
  643. };
  644. static struct genl_ops l2tp_nl_ops[] = {
  645. {
  646. .cmd = L2TP_CMD_NOOP,
  647. .doit = l2tp_nl_cmd_noop,
  648. .policy = l2tp_nl_policy,
  649. /* can be retrieved by unprivileged users */
  650. },
  651. {
  652. .cmd = L2TP_CMD_TUNNEL_CREATE,
  653. .doit = l2tp_nl_cmd_tunnel_create,
  654. .policy = l2tp_nl_policy,
  655. .flags = GENL_ADMIN_PERM,
  656. },
  657. {
  658. .cmd = L2TP_CMD_TUNNEL_DELETE,
  659. .doit = l2tp_nl_cmd_tunnel_delete,
  660. .policy = l2tp_nl_policy,
  661. .flags = GENL_ADMIN_PERM,
  662. },
  663. {
  664. .cmd = L2TP_CMD_TUNNEL_MODIFY,
  665. .doit = l2tp_nl_cmd_tunnel_modify,
  666. .policy = l2tp_nl_policy,
  667. .flags = GENL_ADMIN_PERM,
  668. },
  669. {
  670. .cmd = L2TP_CMD_TUNNEL_GET,
  671. .doit = l2tp_nl_cmd_tunnel_get,
  672. .dumpit = l2tp_nl_cmd_tunnel_dump,
  673. .policy = l2tp_nl_policy,
  674. .flags = GENL_ADMIN_PERM,
  675. },
  676. {
  677. .cmd = L2TP_CMD_SESSION_CREATE,
  678. .doit = l2tp_nl_cmd_session_create,
  679. .policy = l2tp_nl_policy,
  680. .flags = GENL_ADMIN_PERM,
  681. },
  682. {
  683. .cmd = L2TP_CMD_SESSION_DELETE,
  684. .doit = l2tp_nl_cmd_session_delete,
  685. .policy = l2tp_nl_policy,
  686. .flags = GENL_ADMIN_PERM,
  687. },
  688. {
  689. .cmd = L2TP_CMD_SESSION_MODIFY,
  690. .doit = l2tp_nl_cmd_session_modify,
  691. .policy = l2tp_nl_policy,
  692. .flags = GENL_ADMIN_PERM,
  693. },
  694. {
  695. .cmd = L2TP_CMD_SESSION_GET,
  696. .doit = l2tp_nl_cmd_session_get,
  697. .dumpit = l2tp_nl_cmd_session_dump,
  698. .policy = l2tp_nl_policy,
  699. .flags = GENL_ADMIN_PERM,
  700. },
  701. };
  702. int l2tp_nl_register_ops(enum l2tp_pwtype pw_type, const struct l2tp_nl_cmd_ops *ops)
  703. {
  704. int ret;
  705. ret = -EINVAL;
  706. if (pw_type >= __L2TP_PWTYPE_MAX)
  707. goto err;
  708. genl_lock();
  709. ret = -EBUSY;
  710. if (l2tp_nl_cmd_ops[pw_type])
  711. goto out;
  712. l2tp_nl_cmd_ops[pw_type] = ops;
  713. ret = 0;
  714. out:
  715. genl_unlock();
  716. err:
  717. return ret;
  718. }
  719. EXPORT_SYMBOL_GPL(l2tp_nl_register_ops);
  720. void l2tp_nl_unregister_ops(enum l2tp_pwtype pw_type)
  721. {
  722. if (pw_type < __L2TP_PWTYPE_MAX) {
  723. genl_lock();
  724. l2tp_nl_cmd_ops[pw_type] = NULL;
  725. genl_unlock();
  726. }
  727. }
  728. EXPORT_SYMBOL_GPL(l2tp_nl_unregister_ops);
  729. static int l2tp_nl_init(void)
  730. {
  731. int err;
  732. printk(KERN_INFO "L2TP netlink interface\n");
  733. err = genl_register_family_with_ops(&l2tp_nl_family, l2tp_nl_ops,
  734. ARRAY_SIZE(l2tp_nl_ops));
  735. return err;
  736. }
  737. static void l2tp_nl_cleanup(void)
  738. {
  739. genl_unregister_family(&l2tp_nl_family);
  740. }
  741. module_init(l2tp_nl_init);
  742. module_exit(l2tp_nl_cleanup);
  743. MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
  744. MODULE_DESCRIPTION("L2TP netlink");
  745. MODULE_LICENSE("GPL");
  746. MODULE_VERSION("1.0");
  747. MODULE_ALIAS("net-pf-" __stringify(PF_NETLINK) "-proto-" \
  748. __stringify(NETLINK_GENERIC) "-type-" "l2tp");