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