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