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