l2tp_netlink.c 21 KB

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