blob: 24edad0fd9baa6fa8a3e10ee5cb585cfc95bae7e [file] [log] [blame]
James Chapman309795f2010-04-02 06:19:10 +00001/*
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
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
27#include <linux/l2tp.h>
28
29#include "l2tp_core.h"
30
31
32static struct genl_family l2tp_nl_family = {
33 .id = GENL_ID_GENERATE,
34 .name = L2TP_GENL_NAME,
35 .version = L2TP_GENL_VERSION,
36 .hdrsize = 0,
37 .maxattr = L2TP_ATTR_MAX,
38};
39
40/* Accessed under genl lock */
41static const struct l2tp_nl_cmd_ops *l2tp_nl_cmd_ops[__L2TP_PWTYPE_MAX];
42
43static struct l2tp_session *l2tp_nl_session_find(struct genl_info *info)
44{
45 u32 tunnel_id;
46 u32 session_id;
47 char *ifname;
48 struct l2tp_tunnel *tunnel;
49 struct l2tp_session *session = NULL;
50 struct net *net = genl_info_net(info);
51
52 if (info->attrs[L2TP_ATTR_IFNAME]) {
53 ifname = nla_data(info->attrs[L2TP_ATTR_IFNAME]);
54 session = l2tp_session_find_by_ifname(net, ifname);
55 } else if ((info->attrs[L2TP_ATTR_SESSION_ID]) &&
56 (info->attrs[L2TP_ATTR_CONN_ID])) {
57 tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
58 session_id = nla_get_u32(info->attrs[L2TP_ATTR_SESSION_ID]);
59 tunnel = l2tp_tunnel_find(net, tunnel_id);
60 if (tunnel)
61 session = l2tp_session_find(net, tunnel, session_id);
62 }
63
64 return session;
65}
66
67static int l2tp_nl_cmd_noop(struct sk_buff *skb, struct genl_info *info)
68{
69 struct sk_buff *msg;
70 void *hdr;
71 int ret = -ENOBUFS;
72
73 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
74 if (!msg) {
75 ret = -ENOMEM;
76 goto out;
77 }
78
79 hdr = genlmsg_put(msg, info->snd_pid, info->snd_seq,
80 &l2tp_nl_family, 0, L2TP_CMD_NOOP);
81 if (IS_ERR(hdr)) {
82 ret = PTR_ERR(hdr);
83 goto err_out;
84 }
85
86 genlmsg_end(msg, hdr);
87
88 return genlmsg_unicast(genl_info_net(info), msg, info->snd_pid);
89
90err_out:
91 nlmsg_free(msg);
92
93out:
94 return ret;
95}
96
97static int l2tp_nl_cmd_tunnel_create(struct sk_buff *skb, struct genl_info *info)
98{
99 u32 tunnel_id;
100 u32 peer_tunnel_id;
101 int proto_version;
102 int fd;
103 int ret = 0;
104 struct l2tp_tunnel_cfg cfg = { 0, };
105 struct l2tp_tunnel *tunnel;
106 struct net *net = genl_info_net(info);
107
108 if (!info->attrs[L2TP_ATTR_CONN_ID]) {
109 ret = -EINVAL;
110 goto out;
111 }
112 tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
113
114 if (!info->attrs[L2TP_ATTR_PEER_CONN_ID]) {
115 ret = -EINVAL;
116 goto out;
117 }
118 peer_tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_PEER_CONN_ID]);
119
120 if (!info->attrs[L2TP_ATTR_PROTO_VERSION]) {
121 ret = -EINVAL;
122 goto out;
123 }
124 proto_version = nla_get_u8(info->attrs[L2TP_ATTR_PROTO_VERSION]);
125
126 if (!info->attrs[L2TP_ATTR_ENCAP_TYPE]) {
127 ret = -EINVAL;
128 goto out;
129 }
130 cfg.encap = nla_get_u16(info->attrs[L2TP_ATTR_ENCAP_TYPE]);
131
James Chapman789a4a22010-04-02 06:19:40 +0000132 fd = -1;
133 if (info->attrs[L2TP_ATTR_FD]) {
134 fd = nla_get_u32(info->attrs[L2TP_ATTR_FD]);
135 } else {
Chris Elstonf9bac8d2012-04-29 21:48:52 +0000136#if IS_ENABLED(CONFIG_IPV6)
137 if (info->attrs[L2TP_ATTR_IP6_SADDR] &&
138 info->attrs[L2TP_ATTR_IP6_DADDR]) {
139 cfg.local_ip6 = nla_data(
140 info->attrs[L2TP_ATTR_IP6_SADDR]);
141 cfg.peer_ip6 = nla_data(
142 info->attrs[L2TP_ATTR_IP6_DADDR]);
143 } else
144#endif
145 if (info->attrs[L2TP_ATTR_IP_SADDR] &&
146 info->attrs[L2TP_ATTR_IP_DADDR]) {
147 cfg.local_ip.s_addr = nla_get_be32(
148 info->attrs[L2TP_ATTR_IP_SADDR]);
149 cfg.peer_ip.s_addr = nla_get_be32(
150 info->attrs[L2TP_ATTR_IP_DADDR]);
151 } else {
152 ret = -EINVAL;
153 goto out;
154 }
James Chapman789a4a22010-04-02 06:19:40 +0000155 if (info->attrs[L2TP_ATTR_UDP_SPORT])
156 cfg.local_udp_port = nla_get_u16(info->attrs[L2TP_ATTR_UDP_SPORT]);
157 if (info->attrs[L2TP_ATTR_UDP_DPORT])
158 cfg.peer_udp_port = nla_get_u16(info->attrs[L2TP_ATTR_UDP_DPORT]);
159 if (info->attrs[L2TP_ATTR_UDP_CSUM])
160 cfg.use_udp_checksums = nla_get_flag(info->attrs[L2TP_ATTR_UDP_CSUM]);
James Chapman309795f2010-04-02 06:19:10 +0000161 }
James Chapman309795f2010-04-02 06:19:10 +0000162
163 if (info->attrs[L2TP_ATTR_DEBUG])
164 cfg.debug = nla_get_u32(info->attrs[L2TP_ATTR_DEBUG]);
165
166 tunnel = l2tp_tunnel_find(net, tunnel_id);
167 if (tunnel != NULL) {
168 ret = -EEXIST;
169 goto out;
170 }
171
172 ret = -EINVAL;
173 switch (cfg.encap) {
174 case L2TP_ENCAPTYPE_UDP:
175 case L2TP_ENCAPTYPE_IP:
176 ret = l2tp_tunnel_create(net, fd, proto_version, tunnel_id,
177 peer_tunnel_id, &cfg, &tunnel);
178 break;
179 }
180
181out:
182 return ret;
183}
184
185static int l2tp_nl_cmd_tunnel_delete(struct sk_buff *skb, struct genl_info *info)
186{
187 struct l2tp_tunnel *tunnel;
188 u32 tunnel_id;
189 int ret = 0;
190 struct net *net = genl_info_net(info);
191
192 if (!info->attrs[L2TP_ATTR_CONN_ID]) {
193 ret = -EINVAL;
194 goto out;
195 }
196 tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
197
198 tunnel = l2tp_tunnel_find(net, tunnel_id);
199 if (tunnel == NULL) {
200 ret = -ENODEV;
201 goto out;
202 }
203
204 (void) l2tp_tunnel_delete(tunnel);
205
206out:
207 return ret;
208}
209
210static int l2tp_nl_cmd_tunnel_modify(struct sk_buff *skb, struct genl_info *info)
211{
212 struct l2tp_tunnel *tunnel;
213 u32 tunnel_id;
214 int ret = 0;
215 struct net *net = genl_info_net(info);
216
217 if (!info->attrs[L2TP_ATTR_CONN_ID]) {
218 ret = -EINVAL;
219 goto out;
220 }
221 tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
222
223 tunnel = l2tp_tunnel_find(net, tunnel_id);
224 if (tunnel == NULL) {
225 ret = -ENODEV;
226 goto out;
227 }
228
229 if (info->attrs[L2TP_ATTR_DEBUG])
230 tunnel->debug = nla_get_u32(info->attrs[L2TP_ATTR_DEBUG]);
231
232out:
233 return ret;
234}
235
236static int l2tp_nl_tunnel_send(struct sk_buff *skb, u32 pid, u32 seq, int flags,
237 struct l2tp_tunnel *tunnel)
238{
239 void *hdr;
240 struct nlattr *nest;
241 struct sock *sk = NULL;
242 struct inet_sock *inet;
Chris Elstonf9bac8d2012-04-29 21:48:52 +0000243#if IS_ENABLED(CONFIG_IPV6)
244 struct ipv6_pinfo *np = NULL;
245#endif
James Chapman5de7aee2012-04-29 21:48:46 +0000246 struct l2tp_stats stats;
247 unsigned int start;
James Chapman309795f2010-04-02 06:19:10 +0000248
249 hdr = genlmsg_put(skb, pid, seq, &l2tp_nl_family, flags,
250 L2TP_CMD_TUNNEL_GET);
251 if (IS_ERR(hdr))
252 return PTR_ERR(hdr);
253
David S. Miller60aed2a2012-04-01 19:59:31 -0400254 if (nla_put_u8(skb, L2TP_ATTR_PROTO_VERSION, tunnel->version) ||
255 nla_put_u32(skb, L2TP_ATTR_CONN_ID, tunnel->tunnel_id) ||
256 nla_put_u32(skb, L2TP_ATTR_PEER_CONN_ID, tunnel->peer_tunnel_id) ||
257 nla_put_u32(skb, L2TP_ATTR_DEBUG, tunnel->debug) ||
258 nla_put_u16(skb, L2TP_ATTR_ENCAP_TYPE, tunnel->encap))
259 goto nla_put_failure;
James Chapman309795f2010-04-02 06:19:10 +0000260
261 nest = nla_nest_start(skb, L2TP_ATTR_STATS);
262 if (nest == NULL)
263 goto nla_put_failure;
264
James Chapman5de7aee2012-04-29 21:48:46 +0000265 do {
266 start = u64_stats_fetch_begin(&tunnel->stats.syncp);
267 stats.tx_packets = tunnel->stats.tx_packets;
268 stats.tx_bytes = tunnel->stats.tx_bytes;
269 stats.tx_errors = tunnel->stats.tx_errors;
270 stats.rx_packets = tunnel->stats.rx_packets;
271 stats.rx_bytes = tunnel->stats.rx_bytes;
272 stats.rx_errors = tunnel->stats.rx_errors;
273 stats.rx_seq_discards = tunnel->stats.rx_seq_discards;
274 stats.rx_oos_packets = tunnel->stats.rx_oos_packets;
275 } while (u64_stats_fetch_retry(&tunnel->stats.syncp, start));
276
277 if (nla_put_u64(skb, L2TP_ATTR_TX_PACKETS, stats.tx_packets) ||
278 nla_put_u64(skb, L2TP_ATTR_TX_BYTES, stats.tx_bytes) ||
279 nla_put_u64(skb, L2TP_ATTR_TX_ERRORS, stats.tx_errors) ||
280 nla_put_u64(skb, L2TP_ATTR_RX_PACKETS, stats.rx_packets) ||
281 nla_put_u64(skb, L2TP_ATTR_RX_BYTES, stats.rx_bytes) ||
David S. Miller60aed2a2012-04-01 19:59:31 -0400282 nla_put_u64(skb, L2TP_ATTR_RX_SEQ_DISCARDS,
James Chapman5de7aee2012-04-29 21:48:46 +0000283 stats.rx_seq_discards) ||
David S. Miller60aed2a2012-04-01 19:59:31 -0400284 nla_put_u64(skb, L2TP_ATTR_RX_OOS_PACKETS,
James Chapman5de7aee2012-04-29 21:48:46 +0000285 stats.rx_oos_packets) ||
286 nla_put_u64(skb, L2TP_ATTR_RX_ERRORS, stats.rx_errors))
David S. Miller60aed2a2012-04-01 19:59:31 -0400287 goto nla_put_failure;
James Chapman309795f2010-04-02 06:19:10 +0000288 nla_nest_end(skb, nest);
289
290 sk = tunnel->sock;
291 if (!sk)
292 goto out;
293
Chris Elstonf9bac8d2012-04-29 21:48:52 +0000294#if IS_ENABLED(CONFIG_IPV6)
295 if (sk->sk_family == AF_INET6)
296 np = inet6_sk(sk);
297#endif
298
James Chapman309795f2010-04-02 06:19:10 +0000299 inet = inet_sk(sk);
300
301 switch (tunnel->encap) {
302 case L2TP_ENCAPTYPE_UDP:
David S. Miller60aed2a2012-04-01 19:59:31 -0400303 if (nla_put_u16(skb, L2TP_ATTR_UDP_SPORT, ntohs(inet->inet_sport)) ||
304 nla_put_u16(skb, L2TP_ATTR_UDP_DPORT, ntohs(inet->inet_dport)) ||
305 nla_put_u8(skb, L2TP_ATTR_UDP_CSUM,
306 (sk->sk_no_check != UDP_CSUM_NOXMIT)))
307 goto nla_put_failure;
James Chapman309795f2010-04-02 06:19:10 +0000308 /* NOBREAK */
309 case L2TP_ENCAPTYPE_IP:
Chris Elstonf9bac8d2012-04-29 21:48:52 +0000310#if IS_ENABLED(CONFIG_IPV6)
311 if (np) {
312 if (nla_put(skb, L2TP_ATTR_IP6_SADDR, sizeof(np->saddr),
313 &np->saddr) ||
314 nla_put(skb, L2TP_ATTR_IP6_DADDR, sizeof(np->daddr),
315 &np->daddr))
316 goto nla_put_failure;
317 } else
318#endif
David S. Miller60aed2a2012-04-01 19:59:31 -0400319 if (nla_put_be32(skb, L2TP_ATTR_IP_SADDR, inet->inet_saddr) ||
320 nla_put_be32(skb, L2TP_ATTR_IP_DADDR, inet->inet_daddr))
321 goto nla_put_failure;
James Chapman309795f2010-04-02 06:19:10 +0000322 break;
323 }
324
325out:
326 return genlmsg_end(skb, hdr);
327
328nla_put_failure:
329 genlmsg_cancel(skb, hdr);
330 return -1;
331}
332
333static int l2tp_nl_cmd_tunnel_get(struct sk_buff *skb, struct genl_info *info)
334{
335 struct l2tp_tunnel *tunnel;
336 struct sk_buff *msg;
337 u32 tunnel_id;
338 int ret = -ENOBUFS;
339 struct net *net = genl_info_net(info);
340
341 if (!info->attrs[L2TP_ATTR_CONN_ID]) {
342 ret = -EINVAL;
343 goto out;
344 }
345
346 tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
347
348 tunnel = l2tp_tunnel_find(net, tunnel_id);
349 if (tunnel == NULL) {
350 ret = -ENODEV;
351 goto out;
352 }
353
354 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
355 if (!msg) {
356 ret = -ENOMEM;
357 goto out;
358 }
359
360 ret = l2tp_nl_tunnel_send(msg, info->snd_pid, info->snd_seq,
361 NLM_F_ACK, tunnel);
362 if (ret < 0)
363 goto err_out;
364
365 return genlmsg_unicast(net, msg, info->snd_pid);
366
367err_out:
368 nlmsg_free(msg);
369
370out:
371 return ret;
372}
373
374static int l2tp_nl_cmd_tunnel_dump(struct sk_buff *skb, struct netlink_callback *cb)
375{
376 int ti = cb->args[0];
377 struct l2tp_tunnel *tunnel;
378 struct net *net = sock_net(skb->sk);
379
380 for (;;) {
381 tunnel = l2tp_tunnel_find_nth(net, ti);
382 if (tunnel == NULL)
383 goto out;
384
385 if (l2tp_nl_tunnel_send(skb, NETLINK_CB(cb->skb).pid,
386 cb->nlh->nlmsg_seq, NLM_F_MULTI,
387 tunnel) <= 0)
388 goto out;
389
390 ti++;
391 }
392
393out:
394 cb->args[0] = ti;
395
396 return skb->len;
397}
398
399static int l2tp_nl_cmd_session_create(struct sk_buff *skb, struct genl_info *info)
400{
401 u32 tunnel_id = 0;
402 u32 session_id;
403 u32 peer_session_id;
404 int ret = 0;
405 struct l2tp_tunnel *tunnel;
406 struct l2tp_session *session;
407 struct l2tp_session_cfg cfg = { 0, };
408 struct net *net = genl_info_net(info);
409
410 if (!info->attrs[L2TP_ATTR_CONN_ID]) {
411 ret = -EINVAL;
412 goto out;
413 }
414 tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
415 tunnel = l2tp_tunnel_find(net, tunnel_id);
416 if (!tunnel) {
417 ret = -ENODEV;
418 goto out;
419 }
420
421 if (!info->attrs[L2TP_ATTR_SESSION_ID]) {
422 ret = -EINVAL;
423 goto out;
424 }
425 session_id = nla_get_u32(info->attrs[L2TP_ATTR_SESSION_ID]);
426 session = l2tp_session_find(net, tunnel, session_id);
427 if (session) {
428 ret = -EEXIST;
429 goto out;
430 }
431
432 if (!info->attrs[L2TP_ATTR_PEER_SESSION_ID]) {
433 ret = -EINVAL;
434 goto out;
435 }
436 peer_session_id = nla_get_u32(info->attrs[L2TP_ATTR_PEER_SESSION_ID]);
437
438 if (!info->attrs[L2TP_ATTR_PW_TYPE]) {
439 ret = -EINVAL;
440 goto out;
441 }
442 cfg.pw_type = nla_get_u16(info->attrs[L2TP_ATTR_PW_TYPE]);
443 if (cfg.pw_type >= __L2TP_PWTYPE_MAX) {
444 ret = -EINVAL;
445 goto out;
446 }
447
448 if (tunnel->version > 2) {
449 if (info->attrs[L2TP_ATTR_OFFSET])
450 cfg.offset = nla_get_u16(info->attrs[L2TP_ATTR_OFFSET]);
451
452 if (info->attrs[L2TP_ATTR_DATA_SEQ])
453 cfg.data_seq = nla_get_u8(info->attrs[L2TP_ATTR_DATA_SEQ]);
454
455 cfg.l2specific_type = L2TP_L2SPECTYPE_DEFAULT;
456 if (info->attrs[L2TP_ATTR_L2SPEC_TYPE])
457 cfg.l2specific_type = nla_get_u8(info->attrs[L2TP_ATTR_L2SPEC_TYPE]);
458
459 cfg.l2specific_len = 4;
460 if (info->attrs[L2TP_ATTR_L2SPEC_LEN])
461 cfg.l2specific_len = nla_get_u8(info->attrs[L2TP_ATTR_L2SPEC_LEN]);
462
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
484 if (info->attrs[L2TP_ATTR_VLAN_ID])
485 cfg.vlan_id = nla_get_u16(info->attrs[L2TP_ATTR_VLAN_ID]);
486 }
487
488 if (info->attrs[L2TP_ATTR_DEBUG])
489 cfg.debug = nla_get_u32(info->attrs[L2TP_ATTR_DEBUG]);
490
491 if (info->attrs[L2TP_ATTR_RECV_SEQ])
492 cfg.recv_seq = nla_get_u8(info->attrs[L2TP_ATTR_RECV_SEQ]);
493
494 if (info->attrs[L2TP_ATTR_SEND_SEQ])
495 cfg.send_seq = nla_get_u8(info->attrs[L2TP_ATTR_SEND_SEQ]);
496
497 if (info->attrs[L2TP_ATTR_LNS_MODE])
498 cfg.lns_mode = nla_get_u8(info->attrs[L2TP_ATTR_LNS_MODE]);
499
500 if (info->attrs[L2TP_ATTR_RECV_TIMEOUT])
501 cfg.reorder_timeout = nla_get_msecs(info->attrs[L2TP_ATTR_RECV_TIMEOUT]);
502
503 if (info->attrs[L2TP_ATTR_MTU])
504 cfg.mtu = nla_get_u16(info->attrs[L2TP_ATTR_MTU]);
505
506 if (info->attrs[L2TP_ATTR_MRU])
507 cfg.mru = nla_get_u16(info->attrs[L2TP_ATTR_MRU]);
508
509 if ((l2tp_nl_cmd_ops[cfg.pw_type] == NULL) ||
510 (l2tp_nl_cmd_ops[cfg.pw_type]->session_create == NULL)) {
511 ret = -EPROTONOSUPPORT;
512 goto out;
513 }
514
515 /* Check that pseudowire-specific params are present */
516 switch (cfg.pw_type) {
517 case L2TP_PWTYPE_NONE:
518 break;
519 case L2TP_PWTYPE_ETH_VLAN:
520 if (!info->attrs[L2TP_ATTR_VLAN_ID]) {
521 ret = -EINVAL;
522 goto out;
523 }
524 break;
525 case L2TP_PWTYPE_ETH:
526 break;
527 case L2TP_PWTYPE_PPP:
528 case L2TP_PWTYPE_PPP_AC:
529 break;
530 case L2TP_PWTYPE_IP:
531 default:
532 ret = -EPROTONOSUPPORT;
533 break;
534 }
535
536 ret = -EPROTONOSUPPORT;
537 if (l2tp_nl_cmd_ops[cfg.pw_type]->session_create)
538 ret = (*l2tp_nl_cmd_ops[cfg.pw_type]->session_create)(net, tunnel_id,
539 session_id, peer_session_id, &cfg);
540
541out:
542 return ret;
543}
544
545static int l2tp_nl_cmd_session_delete(struct sk_buff *skb, struct genl_info *info)
546{
547 int ret = 0;
548 struct l2tp_session *session;
549 u16 pw_type;
550
551 session = l2tp_nl_session_find(info);
552 if (session == NULL) {
553 ret = -ENODEV;
554 goto out;
555 }
556
557 pw_type = session->pwtype;
558 if (pw_type < __L2TP_PWTYPE_MAX)
559 if (l2tp_nl_cmd_ops[pw_type] && l2tp_nl_cmd_ops[pw_type]->session_delete)
560 ret = (*l2tp_nl_cmd_ops[pw_type]->session_delete)(session);
561
562out:
563 return ret;
564}
565
566static int l2tp_nl_cmd_session_modify(struct sk_buff *skb, struct genl_info *info)
567{
568 int ret = 0;
569 struct l2tp_session *session;
570
571 session = l2tp_nl_session_find(info);
572 if (session == NULL) {
573 ret = -ENODEV;
574 goto out;
575 }
576
577 if (info->attrs[L2TP_ATTR_DEBUG])
578 session->debug = nla_get_u32(info->attrs[L2TP_ATTR_DEBUG]);
579
580 if (info->attrs[L2TP_ATTR_DATA_SEQ])
581 session->data_seq = nla_get_u8(info->attrs[L2TP_ATTR_DATA_SEQ]);
582
583 if (info->attrs[L2TP_ATTR_RECV_SEQ])
584 session->recv_seq = nla_get_u8(info->attrs[L2TP_ATTR_RECV_SEQ]);
585
586 if (info->attrs[L2TP_ATTR_SEND_SEQ])
587 session->send_seq = nla_get_u8(info->attrs[L2TP_ATTR_SEND_SEQ]);
588
589 if (info->attrs[L2TP_ATTR_LNS_MODE])
590 session->lns_mode = nla_get_u8(info->attrs[L2TP_ATTR_LNS_MODE]);
591
592 if (info->attrs[L2TP_ATTR_RECV_TIMEOUT])
593 session->reorder_timeout = nla_get_msecs(info->attrs[L2TP_ATTR_RECV_TIMEOUT]);
594
595 if (info->attrs[L2TP_ATTR_MTU])
596 session->mtu = nla_get_u16(info->attrs[L2TP_ATTR_MTU]);
597
598 if (info->attrs[L2TP_ATTR_MRU])
599 session->mru = nla_get_u16(info->attrs[L2TP_ATTR_MRU]);
600
601out:
602 return ret;
603}
604
605static int l2tp_nl_session_send(struct sk_buff *skb, u32 pid, u32 seq, int flags,
606 struct l2tp_session *session)
607{
608 void *hdr;
609 struct nlattr *nest;
610 struct l2tp_tunnel *tunnel = session->tunnel;
611 struct sock *sk = NULL;
James Chapman5de7aee2012-04-29 21:48:46 +0000612 struct l2tp_stats stats;
613 unsigned int start;
James Chapman309795f2010-04-02 06:19:10 +0000614
615 sk = tunnel->sock;
616
617 hdr = genlmsg_put(skb, pid, seq, &l2tp_nl_family, flags, L2TP_CMD_SESSION_GET);
618 if (IS_ERR(hdr))
619 return PTR_ERR(hdr);
620
David S. Miller60aed2a2012-04-01 19:59:31 -0400621 if (nla_put_u32(skb, L2TP_ATTR_CONN_ID, tunnel->tunnel_id) ||
622 nla_put_u32(skb, L2TP_ATTR_SESSION_ID, session->session_id) ||
623 nla_put_u32(skb, L2TP_ATTR_PEER_CONN_ID, tunnel->peer_tunnel_id) ||
624 nla_put_u32(skb, L2TP_ATTR_PEER_SESSION_ID,
625 session->peer_session_id) ||
626 nla_put_u32(skb, L2TP_ATTR_DEBUG, session->debug) ||
627 nla_put_u16(skb, L2TP_ATTR_PW_TYPE, session->pwtype) ||
628 nla_put_u16(skb, L2TP_ATTR_MTU, session->mtu) ||
629 (session->mru &&
630 nla_put_u16(skb, L2TP_ATTR_MRU, session->mru)))
631 goto nla_put_failure;
James Chapman309795f2010-04-02 06:19:10 +0000632
David S. Miller60aed2a2012-04-01 19:59:31 -0400633 if ((session->ifname && session->ifname[0] &&
634 nla_put_string(skb, L2TP_ATTR_IFNAME, session->ifname)) ||
635 (session->cookie_len &&
636 nla_put(skb, L2TP_ATTR_COOKIE, session->cookie_len,
637 &session->cookie[0])) ||
638 (session->peer_cookie_len &&
639 nla_put(skb, L2TP_ATTR_PEER_COOKIE, session->peer_cookie_len,
640 &session->peer_cookie[0])) ||
641 nla_put_u8(skb, L2TP_ATTR_RECV_SEQ, session->recv_seq) ||
642 nla_put_u8(skb, L2TP_ATTR_SEND_SEQ, session->send_seq) ||
643 nla_put_u8(skb, L2TP_ATTR_LNS_MODE, session->lns_mode) ||
James Chapman309795f2010-04-02 06:19:10 +0000644#ifdef CONFIG_XFRM
David S. Miller60aed2a2012-04-01 19:59:31 -0400645 (((sk) && (sk->sk_policy[0] || sk->sk_policy[1])) &&
646 nla_put_u8(skb, L2TP_ATTR_USING_IPSEC, 1)) ||
James Chapman309795f2010-04-02 06:19:10 +0000647#endif
David S. Miller60aed2a2012-04-01 19:59:31 -0400648 (session->reorder_timeout &&
649 nla_put_msecs(skb, L2TP_ATTR_RECV_TIMEOUT, session->reorder_timeout)))
650 goto nla_put_failure;
James Chapman5de7aee2012-04-29 21:48:46 +0000651
James Chapman309795f2010-04-02 06:19:10 +0000652 nest = nla_nest_start(skb, L2TP_ATTR_STATS);
653 if (nest == NULL)
654 goto nla_put_failure;
James Chapman5de7aee2012-04-29 21:48:46 +0000655
656 do {
657 start = u64_stats_fetch_begin(&session->stats.syncp);
658 stats.tx_packets = session->stats.tx_packets;
659 stats.tx_bytes = session->stats.tx_bytes;
660 stats.tx_errors = session->stats.tx_errors;
661 stats.rx_packets = session->stats.rx_packets;
662 stats.rx_bytes = session->stats.rx_bytes;
663 stats.rx_errors = session->stats.rx_errors;
664 stats.rx_seq_discards = session->stats.rx_seq_discards;
665 stats.rx_oos_packets = session->stats.rx_oos_packets;
666 } while (u64_stats_fetch_retry(&session->stats.syncp, start));
667
668 if (nla_put_u64(skb, L2TP_ATTR_TX_PACKETS, stats.tx_packets) ||
669 nla_put_u64(skb, L2TP_ATTR_TX_BYTES, stats.tx_bytes) ||
670 nla_put_u64(skb, L2TP_ATTR_TX_ERRORS, stats.tx_errors) ||
671 nla_put_u64(skb, L2TP_ATTR_RX_PACKETS, stats.rx_packets) ||
672 nla_put_u64(skb, L2TP_ATTR_RX_BYTES, stats.rx_bytes) ||
David S. Miller60aed2a2012-04-01 19:59:31 -0400673 nla_put_u64(skb, L2TP_ATTR_RX_SEQ_DISCARDS,
James Chapman5de7aee2012-04-29 21:48:46 +0000674 stats.rx_seq_discards) ||
David S. Miller60aed2a2012-04-01 19:59:31 -0400675 nla_put_u64(skb, L2TP_ATTR_RX_OOS_PACKETS,
James Chapman5de7aee2012-04-29 21:48:46 +0000676 stats.rx_oos_packets) ||
677 nla_put_u64(skb, L2TP_ATTR_RX_ERRORS, stats.rx_errors))
David S. Miller60aed2a2012-04-01 19:59:31 -0400678 goto nla_put_failure;
James Chapman309795f2010-04-02 06:19:10 +0000679 nla_nest_end(skb, nest);
680
681 return genlmsg_end(skb, hdr);
682
683 nla_put_failure:
684 genlmsg_cancel(skb, hdr);
685 return -1;
686}
687
688static int l2tp_nl_cmd_session_get(struct sk_buff *skb, struct genl_info *info)
689{
690 struct l2tp_session *session;
691 struct sk_buff *msg;
692 int ret;
693
694 session = l2tp_nl_session_find(info);
695 if (session == NULL) {
696 ret = -ENODEV;
697 goto out;
698 }
699
700 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
701 if (!msg) {
702 ret = -ENOMEM;
703 goto out;
704 }
705
706 ret = l2tp_nl_session_send(msg, info->snd_pid, info->snd_seq,
707 0, session);
708 if (ret < 0)
709 goto err_out;
710
711 return genlmsg_unicast(genl_info_net(info), msg, info->snd_pid);
712
713err_out:
714 nlmsg_free(msg);
715
716out:
717 return ret;
718}
719
720static int l2tp_nl_cmd_session_dump(struct sk_buff *skb, struct netlink_callback *cb)
721{
722 struct net *net = sock_net(skb->sk);
723 struct l2tp_session *session;
724 struct l2tp_tunnel *tunnel = NULL;
725 int ti = cb->args[0];
726 int si = cb->args[1];
727
728 for (;;) {
729 if (tunnel == NULL) {
730 tunnel = l2tp_tunnel_find_nth(net, ti);
731 if (tunnel == NULL)
732 goto out;
733 }
734
735 session = l2tp_session_find_nth(tunnel, si);
736 if (session == NULL) {
737 ti++;
738 tunnel = NULL;
739 si = 0;
740 continue;
741 }
742
743 if (l2tp_nl_session_send(skb, NETLINK_CB(cb->skb).pid,
744 cb->nlh->nlmsg_seq, NLM_F_MULTI,
745 session) <= 0)
746 break;
747
748 si++;
749 }
750
751out:
752 cb->args[0] = ti;
753 cb->args[1] = si;
754
755 return skb->len;
756}
757
758static struct nla_policy l2tp_nl_policy[L2TP_ATTR_MAX + 1] = {
759 [L2TP_ATTR_NONE] = { .type = NLA_UNSPEC, },
760 [L2TP_ATTR_PW_TYPE] = { .type = NLA_U16, },
761 [L2TP_ATTR_ENCAP_TYPE] = { .type = NLA_U16, },
762 [L2TP_ATTR_OFFSET] = { .type = NLA_U16, },
763 [L2TP_ATTR_DATA_SEQ] = { .type = NLA_U8, },
764 [L2TP_ATTR_L2SPEC_TYPE] = { .type = NLA_U8, },
765 [L2TP_ATTR_L2SPEC_LEN] = { .type = NLA_U8, },
766 [L2TP_ATTR_PROTO_VERSION] = { .type = NLA_U8, },
767 [L2TP_ATTR_CONN_ID] = { .type = NLA_U32, },
768 [L2TP_ATTR_PEER_CONN_ID] = { .type = NLA_U32, },
769 [L2TP_ATTR_SESSION_ID] = { .type = NLA_U32, },
770 [L2TP_ATTR_PEER_SESSION_ID] = { .type = NLA_U32, },
771 [L2TP_ATTR_UDP_CSUM] = { .type = NLA_U8, },
772 [L2TP_ATTR_VLAN_ID] = { .type = NLA_U16, },
773 [L2TP_ATTR_DEBUG] = { .type = NLA_U32, },
774 [L2TP_ATTR_RECV_SEQ] = { .type = NLA_U8, },
775 [L2TP_ATTR_SEND_SEQ] = { .type = NLA_U8, },
776 [L2TP_ATTR_LNS_MODE] = { .type = NLA_U8, },
777 [L2TP_ATTR_USING_IPSEC] = { .type = NLA_U8, },
778 [L2TP_ATTR_RECV_TIMEOUT] = { .type = NLA_MSECS, },
779 [L2TP_ATTR_FD] = { .type = NLA_U32, },
780 [L2TP_ATTR_IP_SADDR] = { .type = NLA_U32, },
781 [L2TP_ATTR_IP_DADDR] = { .type = NLA_U32, },
782 [L2TP_ATTR_UDP_SPORT] = { .type = NLA_U16, },
783 [L2TP_ATTR_UDP_DPORT] = { .type = NLA_U16, },
784 [L2TP_ATTR_MTU] = { .type = NLA_U16, },
785 [L2TP_ATTR_MRU] = { .type = NLA_U16, },
786 [L2TP_ATTR_STATS] = { .type = NLA_NESTED, },
Chris Elstonf9bac8d2012-04-29 21:48:52 +0000787 [L2TP_ATTR_IP6_SADDR] = {
788 .type = NLA_BINARY,
789 .len = sizeof(struct in6_addr),
790 },
791 [L2TP_ATTR_IP6_DADDR] = {
792 .type = NLA_BINARY,
793 .len = sizeof(struct in6_addr),
794 },
James Chapman309795f2010-04-02 06:19:10 +0000795 [L2TP_ATTR_IFNAME] = {
796 .type = NLA_NUL_STRING,
797 .len = IFNAMSIZ - 1,
798 },
799 [L2TP_ATTR_COOKIE] = {
800 .type = NLA_BINARY,
801 .len = 8,
802 },
803 [L2TP_ATTR_PEER_COOKIE] = {
804 .type = NLA_BINARY,
805 .len = 8,
806 },
807};
808
809static struct genl_ops l2tp_nl_ops[] = {
810 {
811 .cmd = L2TP_CMD_NOOP,
812 .doit = l2tp_nl_cmd_noop,
813 .policy = l2tp_nl_policy,
814 /* can be retrieved by unprivileged users */
815 },
816 {
817 .cmd = L2TP_CMD_TUNNEL_CREATE,
818 .doit = l2tp_nl_cmd_tunnel_create,
819 .policy = l2tp_nl_policy,
820 .flags = GENL_ADMIN_PERM,
821 },
822 {
823 .cmd = L2TP_CMD_TUNNEL_DELETE,
824 .doit = l2tp_nl_cmd_tunnel_delete,
825 .policy = l2tp_nl_policy,
826 .flags = GENL_ADMIN_PERM,
827 },
828 {
829 .cmd = L2TP_CMD_TUNNEL_MODIFY,
830 .doit = l2tp_nl_cmd_tunnel_modify,
831 .policy = l2tp_nl_policy,
832 .flags = GENL_ADMIN_PERM,
833 },
834 {
835 .cmd = L2TP_CMD_TUNNEL_GET,
836 .doit = l2tp_nl_cmd_tunnel_get,
837 .dumpit = l2tp_nl_cmd_tunnel_dump,
838 .policy = l2tp_nl_policy,
839 .flags = GENL_ADMIN_PERM,
840 },
841 {
842 .cmd = L2TP_CMD_SESSION_CREATE,
843 .doit = l2tp_nl_cmd_session_create,
844 .policy = l2tp_nl_policy,
845 .flags = GENL_ADMIN_PERM,
846 },
847 {
848 .cmd = L2TP_CMD_SESSION_DELETE,
849 .doit = l2tp_nl_cmd_session_delete,
850 .policy = l2tp_nl_policy,
851 .flags = GENL_ADMIN_PERM,
852 },
853 {
854 .cmd = L2TP_CMD_SESSION_MODIFY,
855 .doit = l2tp_nl_cmd_session_modify,
856 .policy = l2tp_nl_policy,
857 .flags = GENL_ADMIN_PERM,
858 },
859 {
860 .cmd = L2TP_CMD_SESSION_GET,
861 .doit = l2tp_nl_cmd_session_get,
862 .dumpit = l2tp_nl_cmd_session_dump,
863 .policy = l2tp_nl_policy,
864 .flags = GENL_ADMIN_PERM,
865 },
866};
867
868int l2tp_nl_register_ops(enum l2tp_pwtype pw_type, const struct l2tp_nl_cmd_ops *ops)
869{
870 int ret;
871
872 ret = -EINVAL;
873 if (pw_type >= __L2TP_PWTYPE_MAX)
874 goto err;
875
876 genl_lock();
877 ret = -EBUSY;
878 if (l2tp_nl_cmd_ops[pw_type])
879 goto out;
880
881 l2tp_nl_cmd_ops[pw_type] = ops;
David S. Miller8cb49012011-04-17 17:01:05 -0700882 ret = 0;
James Chapman309795f2010-04-02 06:19:10 +0000883
884out:
885 genl_unlock();
886err:
David S. Miller8cb49012011-04-17 17:01:05 -0700887 return ret;
James Chapman309795f2010-04-02 06:19:10 +0000888}
889EXPORT_SYMBOL_GPL(l2tp_nl_register_ops);
890
891void l2tp_nl_unregister_ops(enum l2tp_pwtype pw_type)
892{
893 if (pw_type < __L2TP_PWTYPE_MAX) {
894 genl_lock();
895 l2tp_nl_cmd_ops[pw_type] = NULL;
896 genl_unlock();
897 }
898}
899EXPORT_SYMBOL_GPL(l2tp_nl_unregister_ops);
900
901static int l2tp_nl_init(void)
902{
903 int err;
904
905 printk(KERN_INFO "L2TP netlink interface\n");
906 err = genl_register_family_with_ops(&l2tp_nl_family, l2tp_nl_ops,
907 ARRAY_SIZE(l2tp_nl_ops));
908
909 return err;
910}
911
912static void l2tp_nl_cleanup(void)
913{
914 genl_unregister_family(&l2tp_nl_family);
915}
916
917module_init(l2tp_nl_init);
918module_exit(l2tp_nl_cleanup);
919
920MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
921MODULE_DESCRIPTION("L2TP netlink");
922MODULE_LICENSE("GPL");
923MODULE_VERSION("1.0");
924MODULE_ALIAS("net-pf-" __stringify(PF_NETLINK) "-proto-" \
David S. Millerf481c0d2010-04-03 14:58:07 -0700925 __stringify(NETLINK_GENERIC) "-type-" "l2tp");