l2tp : multicast notification to the registered listeners
[cascardo/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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18
19 #include <net/sock.h>
20 #include <net/genetlink.h>
21 #include <net/udp.h>
22 #include <linux/in.h>
23 #include <linux/udp.h>
24 #include <linux/socket.h>
25 #include <linux/module.h>
26 #include <linux/list.h>
27 #include <net/net_namespace.h>
28
29 #include <linux/l2tp.h>
30
31 #include "l2tp_core.h"
32
33
34 static struct genl_family l2tp_nl_family = {
35         .id             = GENL_ID_GENERATE,
36         .name           = L2TP_GENL_NAME,
37         .version        = L2TP_GENL_VERSION,
38         .hdrsize        = 0,
39         .maxattr        = L2TP_ATTR_MAX,
40         .netnsok        = true,
41 };
42
43 static const struct genl_multicast_group l2tp_multicast_group[] = {
44         {
45                 .name = L2TP_GENL_MCGROUP,
46         },
47 };
48
49 static int l2tp_nl_tunnel_send(struct sk_buff *skb, u32 portid, u32 seq,
50                                int flags, struct l2tp_tunnel *tunnel, u8 cmd);
51 static int l2tp_nl_session_send(struct sk_buff *skb, u32 portid, u32 seq,
52                                 int flags, struct l2tp_session *session,
53                                 u8 cmd);
54
55 /* Accessed under genl lock */
56 static const struct l2tp_nl_cmd_ops *l2tp_nl_cmd_ops[__L2TP_PWTYPE_MAX];
57
58 static struct l2tp_session *l2tp_nl_session_find(struct genl_info *info)
59 {
60         u32 tunnel_id;
61         u32 session_id;
62         char *ifname;
63         struct l2tp_tunnel *tunnel;
64         struct l2tp_session *session = NULL;
65         struct net *net = genl_info_net(info);
66
67         if (info->attrs[L2TP_ATTR_IFNAME]) {
68                 ifname = nla_data(info->attrs[L2TP_ATTR_IFNAME]);
69                 session = l2tp_session_find_by_ifname(net, ifname);
70         } else if ((info->attrs[L2TP_ATTR_SESSION_ID]) &&
71                    (info->attrs[L2TP_ATTR_CONN_ID])) {
72                 tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
73                 session_id = nla_get_u32(info->attrs[L2TP_ATTR_SESSION_ID]);
74                 tunnel = l2tp_tunnel_find(net, tunnel_id);
75                 if (tunnel)
76                         session = l2tp_session_find(net, tunnel, session_id);
77         }
78
79         return session;
80 }
81
82 static int l2tp_nl_cmd_noop(struct sk_buff *skb, struct genl_info *info)
83 {
84         struct sk_buff *msg;
85         void *hdr;
86         int ret = -ENOBUFS;
87
88         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
89         if (!msg) {
90                 ret = -ENOMEM;
91                 goto out;
92         }
93
94         hdr = genlmsg_put(msg, info->snd_portid, info->snd_seq,
95                           &l2tp_nl_family, 0, L2TP_CMD_NOOP);
96         if (!hdr) {
97                 ret = -EMSGSIZE;
98                 goto err_out;
99         }
100
101         genlmsg_end(msg, hdr);
102
103         return genlmsg_unicast(genl_info_net(info), msg, info->snd_portid);
104
105 err_out:
106         nlmsg_free(msg);
107
108 out:
109         return ret;
110 }
111
112 static int l2tp_tunnel_notify(struct genl_family *family,
113                               struct genl_info *info,
114                               struct l2tp_tunnel *tunnel,
115                               u8 cmd)
116 {
117         struct sk_buff *msg;
118         int ret;
119
120         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
121         if (!msg)
122                 return -ENOMEM;
123
124         ret = l2tp_nl_tunnel_send(msg, info->snd_portid, info->snd_seq,
125                                   NLM_F_ACK, tunnel, cmd);
126
127         if (ret >= 0)
128                 return genlmsg_multicast_allns(family, msg, 0,  0, GFP_ATOMIC);
129
130         nlmsg_free(msg);
131
132         return ret;
133 }
134
135 static int l2tp_session_notify(struct genl_family *family,
136                                struct genl_info *info,
137                                struct l2tp_session *session,
138                                u8 cmd)
139 {
140         struct sk_buff *msg;
141         int ret;
142
143         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
144         if (!msg)
145                 return -ENOMEM;
146
147         ret = l2tp_nl_session_send(msg, info->snd_portid, info->snd_seq,
148                                    NLM_F_ACK, session, cmd);
149
150         if (ret >= 0)
151                 return genlmsg_multicast_allns(family, msg, 0,  0, GFP_ATOMIC);
152
153         nlmsg_free(msg);
154
155         return ret;
156 }
157
158 static int l2tp_nl_cmd_tunnel_create(struct sk_buff *skb, struct genl_info *info)
159 {
160         u32 tunnel_id;
161         u32 peer_tunnel_id;
162         int proto_version;
163         int fd;
164         int ret = 0;
165         struct l2tp_tunnel_cfg cfg = { 0, };
166         struct l2tp_tunnel *tunnel;
167         struct net *net = genl_info_net(info);
168
169         if (!info->attrs[L2TP_ATTR_CONN_ID]) {
170                 ret = -EINVAL;
171                 goto out;
172         }
173         tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
174
175         if (!info->attrs[L2TP_ATTR_PEER_CONN_ID]) {
176                 ret = -EINVAL;
177                 goto out;
178         }
179         peer_tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_PEER_CONN_ID]);
180
181         if (!info->attrs[L2TP_ATTR_PROTO_VERSION]) {
182                 ret = -EINVAL;
183                 goto out;
184         }
185         proto_version = nla_get_u8(info->attrs[L2TP_ATTR_PROTO_VERSION]);
186
187         if (!info->attrs[L2TP_ATTR_ENCAP_TYPE]) {
188                 ret = -EINVAL;
189                 goto out;
190         }
191         cfg.encap = nla_get_u16(info->attrs[L2TP_ATTR_ENCAP_TYPE]);
192
193         fd = -1;
194         if (info->attrs[L2TP_ATTR_FD]) {
195                 fd = nla_get_u32(info->attrs[L2TP_ATTR_FD]);
196         } else {
197 #if IS_ENABLED(CONFIG_IPV6)
198                 if (info->attrs[L2TP_ATTR_IP6_SADDR] &&
199                     info->attrs[L2TP_ATTR_IP6_DADDR]) {
200                         cfg.local_ip6 = nla_data(
201                                 info->attrs[L2TP_ATTR_IP6_SADDR]);
202                         cfg.peer_ip6 = nla_data(
203                                 info->attrs[L2TP_ATTR_IP6_DADDR]);
204                 } else
205 #endif
206                 if (info->attrs[L2TP_ATTR_IP_SADDR] &&
207                     info->attrs[L2TP_ATTR_IP_DADDR]) {
208                         cfg.local_ip.s_addr = nla_get_be32(
209                                 info->attrs[L2TP_ATTR_IP_SADDR]);
210                         cfg.peer_ip.s_addr = nla_get_be32(
211                                 info->attrs[L2TP_ATTR_IP_DADDR]);
212                 } else {
213                         ret = -EINVAL;
214                         goto out;
215                 }
216                 if (info->attrs[L2TP_ATTR_UDP_SPORT])
217                         cfg.local_udp_port = nla_get_u16(info->attrs[L2TP_ATTR_UDP_SPORT]);
218                 if (info->attrs[L2TP_ATTR_UDP_DPORT])
219                         cfg.peer_udp_port = nla_get_u16(info->attrs[L2TP_ATTR_UDP_DPORT]);
220                 if (info->attrs[L2TP_ATTR_UDP_CSUM])
221                         cfg.use_udp_checksums = nla_get_flag(info->attrs[L2TP_ATTR_UDP_CSUM]);
222
223 #if IS_ENABLED(CONFIG_IPV6)
224                 if (info->attrs[L2TP_ATTR_UDP_ZERO_CSUM6_TX])
225                         cfg.udp6_zero_tx_checksums = nla_get_flag(info->attrs[L2TP_ATTR_UDP_ZERO_CSUM6_TX]);
226                 if (info->attrs[L2TP_ATTR_UDP_ZERO_CSUM6_RX])
227                         cfg.udp6_zero_rx_checksums = nla_get_flag(info->attrs[L2TP_ATTR_UDP_ZERO_CSUM6_RX]);
228 #endif
229         }
230
231         if (info->attrs[L2TP_ATTR_DEBUG])
232                 cfg.debug = nla_get_u32(info->attrs[L2TP_ATTR_DEBUG]);
233
234         tunnel = l2tp_tunnel_find(net, tunnel_id);
235         if (tunnel != NULL) {
236                 ret = -EEXIST;
237                 goto out;
238         }
239
240         ret = -EINVAL;
241         switch (cfg.encap) {
242         case L2TP_ENCAPTYPE_UDP:
243         case L2TP_ENCAPTYPE_IP:
244                 ret = l2tp_tunnel_create(net, fd, proto_version, tunnel_id,
245                                          peer_tunnel_id, &cfg, &tunnel);
246                 break;
247         }
248
249         if (ret >= 0)
250                 ret = l2tp_tunnel_notify(&l2tp_nl_family, info,
251                                          tunnel, L2TP_CMD_TUNNEL_CREATE);
252 out:
253         return ret;
254 }
255
256 static int l2tp_nl_cmd_tunnel_delete(struct sk_buff *skb, struct genl_info *info)
257 {
258         struct l2tp_tunnel *tunnel;
259         u32 tunnel_id;
260         int ret = 0;
261         struct net *net = genl_info_net(info);
262
263         if (!info->attrs[L2TP_ATTR_CONN_ID]) {
264                 ret = -EINVAL;
265                 goto out;
266         }
267         tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
268
269         tunnel = l2tp_tunnel_find(net, tunnel_id);
270         if (tunnel == NULL) {
271                 ret = -ENODEV;
272                 goto out;
273         }
274
275         l2tp_tunnel_notify(&l2tp_nl_family, info,
276                            tunnel, L2TP_CMD_TUNNEL_DELETE);
277
278         (void) l2tp_tunnel_delete(tunnel);
279
280 out:
281         return ret;
282 }
283
284 static int l2tp_nl_cmd_tunnel_modify(struct sk_buff *skb, struct genl_info *info)
285 {
286         struct l2tp_tunnel *tunnel;
287         u32 tunnel_id;
288         int ret = 0;
289         struct net *net = genl_info_net(info);
290
291         if (!info->attrs[L2TP_ATTR_CONN_ID]) {
292                 ret = -EINVAL;
293                 goto out;
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         if (info->attrs[L2TP_ATTR_DEBUG])
304                 tunnel->debug = nla_get_u32(info->attrs[L2TP_ATTR_DEBUG]);
305
306         ret = l2tp_tunnel_notify(&l2tp_nl_family, info,
307                                  tunnel, L2TP_CMD_TUNNEL_MODIFY);
308
309 out:
310         return ret;
311 }
312
313 static int l2tp_nl_tunnel_send(struct sk_buff *skb, u32 portid, u32 seq, int flags,
314                                struct l2tp_tunnel *tunnel, u8 cmd)
315 {
316         void *hdr;
317         struct nlattr *nest;
318         struct sock *sk = NULL;
319         struct inet_sock *inet;
320 #if IS_ENABLED(CONFIG_IPV6)
321         struct ipv6_pinfo *np = NULL;
322 #endif
323
324         hdr = genlmsg_put(skb, portid, seq, &l2tp_nl_family, flags, cmd);
325         if (!hdr)
326                 return -EMSGSIZE;
327
328         if (nla_put_u8(skb, L2TP_ATTR_PROTO_VERSION, tunnel->version) ||
329             nla_put_u32(skb, L2TP_ATTR_CONN_ID, tunnel->tunnel_id) ||
330             nla_put_u32(skb, L2TP_ATTR_PEER_CONN_ID, tunnel->peer_tunnel_id) ||
331             nla_put_u32(skb, L2TP_ATTR_DEBUG, tunnel->debug) ||
332             nla_put_u16(skb, L2TP_ATTR_ENCAP_TYPE, tunnel->encap))
333                 goto nla_put_failure;
334
335         nest = nla_nest_start(skb, L2TP_ATTR_STATS);
336         if (nest == NULL)
337                 goto nla_put_failure;
338
339         if (nla_put_u64(skb, L2TP_ATTR_TX_PACKETS,
340                     atomic_long_read(&tunnel->stats.tx_packets)) ||
341             nla_put_u64(skb, L2TP_ATTR_TX_BYTES,
342                     atomic_long_read(&tunnel->stats.tx_bytes)) ||
343             nla_put_u64(skb, L2TP_ATTR_TX_ERRORS,
344                     atomic_long_read(&tunnel->stats.tx_errors)) ||
345             nla_put_u64(skb, L2TP_ATTR_RX_PACKETS,
346                     atomic_long_read(&tunnel->stats.rx_packets)) ||
347             nla_put_u64(skb, L2TP_ATTR_RX_BYTES,
348                     atomic_long_read(&tunnel->stats.rx_bytes)) ||
349             nla_put_u64(skb, L2TP_ATTR_RX_SEQ_DISCARDS,
350                     atomic_long_read(&tunnel->stats.rx_seq_discards)) ||
351             nla_put_u64(skb, L2TP_ATTR_RX_OOS_PACKETS,
352                     atomic_long_read(&tunnel->stats.rx_oos_packets)) ||
353             nla_put_u64(skb, L2TP_ATTR_RX_ERRORS,
354                     atomic_long_read(&tunnel->stats.rx_errors)))
355                 goto nla_put_failure;
356         nla_nest_end(skb, nest);
357
358         sk = tunnel->sock;
359         if (!sk)
360                 goto out;
361
362 #if IS_ENABLED(CONFIG_IPV6)
363         if (sk->sk_family == AF_INET6)
364                 np = inet6_sk(sk);
365 #endif
366
367         inet = inet_sk(sk);
368
369         switch (tunnel->encap) {
370         case L2TP_ENCAPTYPE_UDP:
371                 if (nla_put_u16(skb, L2TP_ATTR_UDP_SPORT, ntohs(inet->inet_sport)) ||
372                     nla_put_u16(skb, L2TP_ATTR_UDP_DPORT, ntohs(inet->inet_dport)) ||
373                     nla_put_u8(skb, L2TP_ATTR_UDP_CSUM, !sk->sk_no_check_tx))
374                         goto nla_put_failure;
375                 /* NOBREAK */
376         case L2TP_ENCAPTYPE_IP:
377 #if IS_ENABLED(CONFIG_IPV6)
378                 if (np) {
379                         if (nla_put(skb, L2TP_ATTR_IP6_SADDR, sizeof(np->saddr),
380                                     &np->saddr) ||
381                             nla_put(skb, L2TP_ATTR_IP6_DADDR, sizeof(sk->sk_v6_daddr),
382                                     &sk->sk_v6_daddr))
383                                 goto nla_put_failure;
384                 } else
385 #endif
386                 if (nla_put_be32(skb, L2TP_ATTR_IP_SADDR, inet->inet_saddr) ||
387                     nla_put_be32(skb, L2TP_ATTR_IP_DADDR, inet->inet_daddr))
388                         goto nla_put_failure;
389                 break;
390         }
391
392 out:
393         return genlmsg_end(skb, hdr);
394
395 nla_put_failure:
396         genlmsg_cancel(skb, hdr);
397         return -1;
398 }
399
400 static int l2tp_nl_cmd_tunnel_get(struct sk_buff *skb, struct genl_info *info)
401 {
402         struct l2tp_tunnel *tunnel;
403         struct sk_buff *msg;
404         u32 tunnel_id;
405         int ret = -ENOBUFS;
406         struct net *net = genl_info_net(info);
407
408         if (!info->attrs[L2TP_ATTR_CONN_ID]) {
409                 ret = -EINVAL;
410                 goto out;
411         }
412
413         tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
414
415         tunnel = l2tp_tunnel_find(net, tunnel_id);
416         if (tunnel == NULL) {
417                 ret = -ENODEV;
418                 goto out;
419         }
420
421         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
422         if (!msg) {
423                 ret = -ENOMEM;
424                 goto out;
425         }
426
427         ret = l2tp_nl_tunnel_send(msg, info->snd_portid, info->snd_seq,
428                                   NLM_F_ACK, tunnel, L2TP_CMD_TUNNEL_GET);
429         if (ret < 0)
430                 goto err_out;
431
432         return genlmsg_unicast(net, msg, info->snd_portid);
433
434 err_out:
435         nlmsg_free(msg);
436
437 out:
438         return ret;
439 }
440
441 static int l2tp_nl_cmd_tunnel_dump(struct sk_buff *skb, struct netlink_callback *cb)
442 {
443         int ti = cb->args[0];
444         struct l2tp_tunnel *tunnel;
445         struct net *net = sock_net(skb->sk);
446
447         for (;;) {
448                 tunnel = l2tp_tunnel_find_nth(net, ti);
449                 if (tunnel == NULL)
450                         goto out;
451
452                 if (l2tp_nl_tunnel_send(skb, NETLINK_CB(cb->skb).portid,
453                                         cb->nlh->nlmsg_seq, NLM_F_MULTI,
454                                         tunnel, L2TP_CMD_TUNNEL_GET) <= 0)
455                         goto out;
456
457                 ti++;
458         }
459
460 out:
461         cb->args[0] = ti;
462
463         return skb->len;
464 }
465
466 static int l2tp_nl_cmd_session_create(struct sk_buff *skb, struct genl_info *info)
467 {
468         u32 tunnel_id = 0;
469         u32 session_id;
470         u32 peer_session_id;
471         int ret = 0;
472         struct l2tp_tunnel *tunnel;
473         struct l2tp_session *session;
474         struct l2tp_session_cfg cfg = { 0, };
475         struct net *net = genl_info_net(info);
476
477         if (!info->attrs[L2TP_ATTR_CONN_ID]) {
478                 ret = -EINVAL;
479                 goto out;
480         }
481         tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
482         tunnel = l2tp_tunnel_find(net, tunnel_id);
483         if (!tunnel) {
484                 ret = -ENODEV;
485                 goto out;
486         }
487
488         if (!info->attrs[L2TP_ATTR_SESSION_ID]) {
489                 ret = -EINVAL;
490                 goto out;
491         }
492         session_id = nla_get_u32(info->attrs[L2TP_ATTR_SESSION_ID]);
493         session = l2tp_session_find(net, tunnel, session_id);
494         if (session) {
495                 ret = -EEXIST;
496                 goto out;
497         }
498
499         if (!info->attrs[L2TP_ATTR_PEER_SESSION_ID]) {
500                 ret = -EINVAL;
501                 goto out;
502         }
503         peer_session_id = nla_get_u32(info->attrs[L2TP_ATTR_PEER_SESSION_ID]);
504
505         if (!info->attrs[L2TP_ATTR_PW_TYPE]) {
506                 ret = -EINVAL;
507                 goto out;
508         }
509         cfg.pw_type = nla_get_u16(info->attrs[L2TP_ATTR_PW_TYPE]);
510         if (cfg.pw_type >= __L2TP_PWTYPE_MAX) {
511                 ret = -EINVAL;
512                 goto out;
513         }
514
515         if (tunnel->version > 2) {
516                 if (info->attrs[L2TP_ATTR_OFFSET])
517                         cfg.offset = nla_get_u16(info->attrs[L2TP_ATTR_OFFSET]);
518
519                 if (info->attrs[L2TP_ATTR_DATA_SEQ])
520                         cfg.data_seq = nla_get_u8(info->attrs[L2TP_ATTR_DATA_SEQ]);
521
522                 cfg.l2specific_type = L2TP_L2SPECTYPE_DEFAULT;
523                 if (info->attrs[L2TP_ATTR_L2SPEC_TYPE])
524                         cfg.l2specific_type = nla_get_u8(info->attrs[L2TP_ATTR_L2SPEC_TYPE]);
525
526                 cfg.l2specific_len = 4;
527                 if (info->attrs[L2TP_ATTR_L2SPEC_LEN])
528                         cfg.l2specific_len = nla_get_u8(info->attrs[L2TP_ATTR_L2SPEC_LEN]);
529
530                 if (info->attrs[L2TP_ATTR_COOKIE]) {
531                         u16 len = nla_len(info->attrs[L2TP_ATTR_COOKIE]);
532                         if (len > 8) {
533                                 ret = -EINVAL;
534                                 goto out;
535                         }
536                         cfg.cookie_len = len;
537                         memcpy(&cfg.cookie[0], nla_data(info->attrs[L2TP_ATTR_COOKIE]), len);
538                 }
539                 if (info->attrs[L2TP_ATTR_PEER_COOKIE]) {
540                         u16 len = nla_len(info->attrs[L2TP_ATTR_PEER_COOKIE]);
541                         if (len > 8) {
542                                 ret = -EINVAL;
543                                 goto out;
544                         }
545                         cfg.peer_cookie_len = len;
546                         memcpy(&cfg.peer_cookie[0], nla_data(info->attrs[L2TP_ATTR_PEER_COOKIE]), len);
547                 }
548                 if (info->attrs[L2TP_ATTR_IFNAME])
549                         cfg.ifname = nla_data(info->attrs[L2TP_ATTR_IFNAME]);
550
551                 if (info->attrs[L2TP_ATTR_VLAN_ID])
552                         cfg.vlan_id = nla_get_u16(info->attrs[L2TP_ATTR_VLAN_ID]);
553         }
554
555         if (info->attrs[L2TP_ATTR_DEBUG])
556                 cfg.debug = nla_get_u32(info->attrs[L2TP_ATTR_DEBUG]);
557
558         if (info->attrs[L2TP_ATTR_RECV_SEQ])
559                 cfg.recv_seq = nla_get_u8(info->attrs[L2TP_ATTR_RECV_SEQ]);
560
561         if (info->attrs[L2TP_ATTR_SEND_SEQ])
562                 cfg.send_seq = nla_get_u8(info->attrs[L2TP_ATTR_SEND_SEQ]);
563
564         if (info->attrs[L2TP_ATTR_LNS_MODE])
565                 cfg.lns_mode = nla_get_u8(info->attrs[L2TP_ATTR_LNS_MODE]);
566
567         if (info->attrs[L2TP_ATTR_RECV_TIMEOUT])
568                 cfg.reorder_timeout = nla_get_msecs(info->attrs[L2TP_ATTR_RECV_TIMEOUT]);
569
570         if (info->attrs[L2TP_ATTR_MTU])
571                 cfg.mtu = nla_get_u16(info->attrs[L2TP_ATTR_MTU]);
572
573         if (info->attrs[L2TP_ATTR_MRU])
574                 cfg.mru = nla_get_u16(info->attrs[L2TP_ATTR_MRU]);
575
576         if ((l2tp_nl_cmd_ops[cfg.pw_type] == NULL) ||
577             (l2tp_nl_cmd_ops[cfg.pw_type]->session_create == NULL)) {
578                 ret = -EPROTONOSUPPORT;
579                 goto out;
580         }
581
582         /* Check that pseudowire-specific params are present */
583         switch (cfg.pw_type) {
584         case L2TP_PWTYPE_NONE:
585                 break;
586         case L2TP_PWTYPE_ETH_VLAN:
587                 if (!info->attrs[L2TP_ATTR_VLAN_ID]) {
588                         ret = -EINVAL;
589                         goto out;
590                 }
591                 break;
592         case L2TP_PWTYPE_ETH:
593                 break;
594         case L2TP_PWTYPE_PPP:
595         case L2TP_PWTYPE_PPP_AC:
596                 break;
597         case L2TP_PWTYPE_IP:
598         default:
599                 ret = -EPROTONOSUPPORT;
600                 break;
601         }
602
603         ret = -EPROTONOSUPPORT;
604         if (l2tp_nl_cmd_ops[cfg.pw_type]->session_create)
605                 ret = (*l2tp_nl_cmd_ops[cfg.pw_type]->session_create)(net, tunnel_id,
606                         session_id, peer_session_id, &cfg);
607
608         if (ret >= 0) {
609                 session = l2tp_session_find(net, tunnel, session_id);
610                 if (session)
611                         ret = l2tp_session_notify(&l2tp_nl_family, info, session,
612                                                   L2TP_CMD_SESSION_CREATE);
613         }
614
615 out:
616         return ret;
617 }
618
619 static int l2tp_nl_cmd_session_delete(struct sk_buff *skb, struct genl_info *info)
620 {
621         int ret = 0;
622         struct l2tp_session *session;
623         u16 pw_type;
624
625         session = l2tp_nl_session_find(info);
626         if (session == NULL) {
627                 ret = -ENODEV;
628                 goto out;
629         }
630
631         l2tp_session_notify(&l2tp_nl_family, info,
632                             session, L2TP_CMD_SESSION_DELETE);
633
634         pw_type = session->pwtype;
635         if (pw_type < __L2TP_PWTYPE_MAX)
636                 if (l2tp_nl_cmd_ops[pw_type] && l2tp_nl_cmd_ops[pw_type]->session_delete)
637                         ret = (*l2tp_nl_cmd_ops[pw_type]->session_delete)(session);
638
639 out:
640         return ret;
641 }
642
643 static int l2tp_nl_cmd_session_modify(struct sk_buff *skb, struct genl_info *info)
644 {
645         int ret = 0;
646         struct l2tp_session *session;
647
648         session = l2tp_nl_session_find(info);
649         if (session == NULL) {
650                 ret = -ENODEV;
651                 goto out;
652         }
653
654         if (info->attrs[L2TP_ATTR_DEBUG])
655                 session->debug = nla_get_u32(info->attrs[L2TP_ATTR_DEBUG]);
656
657         if (info->attrs[L2TP_ATTR_DATA_SEQ])
658                 session->data_seq = nla_get_u8(info->attrs[L2TP_ATTR_DATA_SEQ]);
659
660         if (info->attrs[L2TP_ATTR_RECV_SEQ])
661                 session->recv_seq = nla_get_u8(info->attrs[L2TP_ATTR_RECV_SEQ]);
662
663         if (info->attrs[L2TP_ATTR_SEND_SEQ]) {
664                 session->send_seq = nla_get_u8(info->attrs[L2TP_ATTR_SEND_SEQ]);
665                 l2tp_session_set_header_len(session, session->tunnel->version);
666         }
667
668         if (info->attrs[L2TP_ATTR_LNS_MODE])
669                 session->lns_mode = nla_get_u8(info->attrs[L2TP_ATTR_LNS_MODE]);
670
671         if (info->attrs[L2TP_ATTR_RECV_TIMEOUT])
672                 session->reorder_timeout = nla_get_msecs(info->attrs[L2TP_ATTR_RECV_TIMEOUT]);
673
674         if (info->attrs[L2TP_ATTR_MTU])
675                 session->mtu = nla_get_u16(info->attrs[L2TP_ATTR_MTU]);
676
677         if (info->attrs[L2TP_ATTR_MRU])
678                 session->mru = nla_get_u16(info->attrs[L2TP_ATTR_MRU]);
679
680         ret = l2tp_session_notify(&l2tp_nl_family, info,
681                                   session, L2TP_CMD_SESSION_MODIFY);
682
683 out:
684         return ret;
685 }
686
687 static int l2tp_nl_session_send(struct sk_buff *skb, u32 portid, u32 seq, int flags,
688                                 struct l2tp_session *session, u8 cmd)
689 {
690         void *hdr;
691         struct nlattr *nest;
692         struct l2tp_tunnel *tunnel = session->tunnel;
693         struct sock *sk = NULL;
694
695         sk = tunnel->sock;
696
697         hdr = genlmsg_put(skb, portid, seq, &l2tp_nl_family, flags, cmd);
698         if (!hdr)
699                 return -EMSGSIZE;
700
701         if (nla_put_u32(skb, L2TP_ATTR_CONN_ID, tunnel->tunnel_id) ||
702             nla_put_u32(skb, L2TP_ATTR_SESSION_ID, session->session_id) ||
703             nla_put_u32(skb, L2TP_ATTR_PEER_CONN_ID, tunnel->peer_tunnel_id) ||
704             nla_put_u32(skb, L2TP_ATTR_PEER_SESSION_ID,
705                         session->peer_session_id) ||
706             nla_put_u32(skb, L2TP_ATTR_DEBUG, session->debug) ||
707             nla_put_u16(skb, L2TP_ATTR_PW_TYPE, session->pwtype) ||
708             nla_put_u16(skb, L2TP_ATTR_MTU, session->mtu) ||
709             (session->mru &&
710              nla_put_u16(skb, L2TP_ATTR_MRU, session->mru)))
711                 goto nla_put_failure;
712
713         if ((session->ifname[0] &&
714              nla_put_string(skb, L2TP_ATTR_IFNAME, session->ifname)) ||
715             (session->cookie_len &&
716              nla_put(skb, L2TP_ATTR_COOKIE, session->cookie_len,
717                      &session->cookie[0])) ||
718             (session->peer_cookie_len &&
719              nla_put(skb, L2TP_ATTR_PEER_COOKIE, session->peer_cookie_len,
720                      &session->peer_cookie[0])) ||
721             nla_put_u8(skb, L2TP_ATTR_RECV_SEQ, session->recv_seq) ||
722             nla_put_u8(skb, L2TP_ATTR_SEND_SEQ, session->send_seq) ||
723             nla_put_u8(skb, L2TP_ATTR_LNS_MODE, session->lns_mode) ||
724 #ifdef CONFIG_XFRM
725             (((sk) && (sk->sk_policy[0] || sk->sk_policy[1])) &&
726              nla_put_u8(skb, L2TP_ATTR_USING_IPSEC, 1)) ||
727 #endif
728             (session->reorder_timeout &&
729              nla_put_msecs(skb, L2TP_ATTR_RECV_TIMEOUT, session->reorder_timeout)))
730                 goto nla_put_failure;
731
732         nest = nla_nest_start(skb, L2TP_ATTR_STATS);
733         if (nest == NULL)
734                 goto nla_put_failure;
735
736         if (nla_put_u64(skb, L2TP_ATTR_TX_PACKETS,
737                 atomic_long_read(&session->stats.tx_packets)) ||
738             nla_put_u64(skb, L2TP_ATTR_TX_BYTES,
739                 atomic_long_read(&session->stats.tx_bytes)) ||
740             nla_put_u64(skb, L2TP_ATTR_TX_ERRORS,
741                 atomic_long_read(&session->stats.tx_errors)) ||
742             nla_put_u64(skb, L2TP_ATTR_RX_PACKETS,
743                 atomic_long_read(&session->stats.rx_packets)) ||
744             nla_put_u64(skb, L2TP_ATTR_RX_BYTES,
745                 atomic_long_read(&session->stats.rx_bytes)) ||
746             nla_put_u64(skb, L2TP_ATTR_RX_SEQ_DISCARDS,
747                 atomic_long_read(&session->stats.rx_seq_discards)) ||
748             nla_put_u64(skb, L2TP_ATTR_RX_OOS_PACKETS,
749                 atomic_long_read(&session->stats.rx_oos_packets)) ||
750             nla_put_u64(skb, L2TP_ATTR_RX_ERRORS,
751                 atomic_long_read(&session->stats.rx_errors)))
752                 goto nla_put_failure;
753         nla_nest_end(skb, nest);
754
755         return genlmsg_end(skb, hdr);
756
757  nla_put_failure:
758         genlmsg_cancel(skb, hdr);
759         return -1;
760 }
761
762 static int l2tp_nl_cmd_session_get(struct sk_buff *skb, struct genl_info *info)
763 {
764         struct l2tp_session *session;
765         struct sk_buff *msg;
766         int ret;
767
768         session = l2tp_nl_session_find(info);
769         if (session == NULL) {
770                 ret = -ENODEV;
771                 goto out;
772         }
773
774         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
775         if (!msg) {
776                 ret = -ENOMEM;
777                 goto out;
778         }
779
780         ret = l2tp_nl_session_send(msg, info->snd_portid, info->snd_seq,
781                                    0, session, L2TP_CMD_SESSION_GET);
782         if (ret < 0)
783                 goto err_out;
784
785         return genlmsg_unicast(genl_info_net(info), msg, info->snd_portid);
786
787 err_out:
788         nlmsg_free(msg);
789
790 out:
791         return ret;
792 }
793
794 static int l2tp_nl_cmd_session_dump(struct sk_buff *skb, struct netlink_callback *cb)
795 {
796         struct net *net = sock_net(skb->sk);
797         struct l2tp_session *session;
798         struct l2tp_tunnel *tunnel = NULL;
799         int ti = cb->args[0];
800         int si = cb->args[1];
801
802         for (;;) {
803                 if (tunnel == NULL) {
804                         tunnel = l2tp_tunnel_find_nth(net, ti);
805                         if (tunnel == NULL)
806                                 goto out;
807                 }
808
809                 session = l2tp_session_find_nth(tunnel, si);
810                 if (session == NULL) {
811                         ti++;
812                         tunnel = NULL;
813                         si = 0;
814                         continue;
815                 }
816
817                 if (l2tp_nl_session_send(skb, NETLINK_CB(cb->skb).portid,
818                                          cb->nlh->nlmsg_seq, NLM_F_MULTI,
819                                          session, L2TP_CMD_SESSION_GET) <= 0)
820                         break;
821
822                 si++;
823         }
824
825 out:
826         cb->args[0] = ti;
827         cb->args[1] = si;
828
829         return skb->len;
830 }
831
832 static struct nla_policy l2tp_nl_policy[L2TP_ATTR_MAX + 1] = {
833         [L2TP_ATTR_NONE]                = { .type = NLA_UNSPEC, },
834         [L2TP_ATTR_PW_TYPE]             = { .type = NLA_U16, },
835         [L2TP_ATTR_ENCAP_TYPE]          = { .type = NLA_U16, },
836         [L2TP_ATTR_OFFSET]              = { .type = NLA_U16, },
837         [L2TP_ATTR_DATA_SEQ]            = { .type = NLA_U8, },
838         [L2TP_ATTR_L2SPEC_TYPE]         = { .type = NLA_U8, },
839         [L2TP_ATTR_L2SPEC_LEN]          = { .type = NLA_U8, },
840         [L2TP_ATTR_PROTO_VERSION]       = { .type = NLA_U8, },
841         [L2TP_ATTR_CONN_ID]             = { .type = NLA_U32, },
842         [L2TP_ATTR_PEER_CONN_ID]        = { .type = NLA_U32, },
843         [L2TP_ATTR_SESSION_ID]          = { .type = NLA_U32, },
844         [L2TP_ATTR_PEER_SESSION_ID]     = { .type = NLA_U32, },
845         [L2TP_ATTR_UDP_CSUM]            = { .type = NLA_U8, },
846         [L2TP_ATTR_VLAN_ID]             = { .type = NLA_U16, },
847         [L2TP_ATTR_DEBUG]               = { .type = NLA_U32, },
848         [L2TP_ATTR_RECV_SEQ]            = { .type = NLA_U8, },
849         [L2TP_ATTR_SEND_SEQ]            = { .type = NLA_U8, },
850         [L2TP_ATTR_LNS_MODE]            = { .type = NLA_U8, },
851         [L2TP_ATTR_USING_IPSEC]         = { .type = NLA_U8, },
852         [L2TP_ATTR_RECV_TIMEOUT]        = { .type = NLA_MSECS, },
853         [L2TP_ATTR_FD]                  = { .type = NLA_U32, },
854         [L2TP_ATTR_IP_SADDR]            = { .type = NLA_U32, },
855         [L2TP_ATTR_IP_DADDR]            = { .type = NLA_U32, },
856         [L2TP_ATTR_UDP_SPORT]           = { .type = NLA_U16, },
857         [L2TP_ATTR_UDP_DPORT]           = { .type = NLA_U16, },
858         [L2TP_ATTR_MTU]                 = { .type = NLA_U16, },
859         [L2TP_ATTR_MRU]                 = { .type = NLA_U16, },
860         [L2TP_ATTR_STATS]               = { .type = NLA_NESTED, },
861         [L2TP_ATTR_IP6_SADDR] = {
862                 .type = NLA_BINARY,
863                 .len = sizeof(struct in6_addr),
864         },
865         [L2TP_ATTR_IP6_DADDR] = {
866                 .type = NLA_BINARY,
867                 .len = sizeof(struct in6_addr),
868         },
869         [L2TP_ATTR_IFNAME] = {
870                 .type = NLA_NUL_STRING,
871                 .len = IFNAMSIZ - 1,
872         },
873         [L2TP_ATTR_COOKIE] = {
874                 .type = NLA_BINARY,
875                 .len = 8,
876         },
877         [L2TP_ATTR_PEER_COOKIE] = {
878                 .type = NLA_BINARY,
879                 .len = 8,
880         },
881 };
882
883 static const struct genl_ops l2tp_nl_ops[] = {
884         {
885                 .cmd = L2TP_CMD_NOOP,
886                 .doit = l2tp_nl_cmd_noop,
887                 .policy = l2tp_nl_policy,
888                 /* can be retrieved by unprivileged users */
889         },
890         {
891                 .cmd = L2TP_CMD_TUNNEL_CREATE,
892                 .doit = l2tp_nl_cmd_tunnel_create,
893                 .policy = l2tp_nl_policy,
894                 .flags = GENL_ADMIN_PERM,
895         },
896         {
897                 .cmd = L2TP_CMD_TUNNEL_DELETE,
898                 .doit = l2tp_nl_cmd_tunnel_delete,
899                 .policy = l2tp_nl_policy,
900                 .flags = GENL_ADMIN_PERM,
901         },
902         {
903                 .cmd = L2TP_CMD_TUNNEL_MODIFY,
904                 .doit = l2tp_nl_cmd_tunnel_modify,
905                 .policy = l2tp_nl_policy,
906                 .flags = GENL_ADMIN_PERM,
907         },
908         {
909                 .cmd = L2TP_CMD_TUNNEL_GET,
910                 .doit = l2tp_nl_cmd_tunnel_get,
911                 .dumpit = l2tp_nl_cmd_tunnel_dump,
912                 .policy = l2tp_nl_policy,
913                 .flags = GENL_ADMIN_PERM,
914         },
915         {
916                 .cmd = L2TP_CMD_SESSION_CREATE,
917                 .doit = l2tp_nl_cmd_session_create,
918                 .policy = l2tp_nl_policy,
919                 .flags = GENL_ADMIN_PERM,
920         },
921         {
922                 .cmd = L2TP_CMD_SESSION_DELETE,
923                 .doit = l2tp_nl_cmd_session_delete,
924                 .policy = l2tp_nl_policy,
925                 .flags = GENL_ADMIN_PERM,
926         },
927         {
928                 .cmd = L2TP_CMD_SESSION_MODIFY,
929                 .doit = l2tp_nl_cmd_session_modify,
930                 .policy = l2tp_nl_policy,
931                 .flags = GENL_ADMIN_PERM,
932         },
933         {
934                 .cmd = L2TP_CMD_SESSION_GET,
935                 .doit = l2tp_nl_cmd_session_get,
936                 .dumpit = l2tp_nl_cmd_session_dump,
937                 .policy = l2tp_nl_policy,
938                 .flags = GENL_ADMIN_PERM,
939         },
940 };
941
942 int l2tp_nl_register_ops(enum l2tp_pwtype pw_type, const struct l2tp_nl_cmd_ops *ops)
943 {
944         int ret;
945
946         ret = -EINVAL;
947         if (pw_type >= __L2TP_PWTYPE_MAX)
948                 goto err;
949
950         genl_lock();
951         ret = -EBUSY;
952         if (l2tp_nl_cmd_ops[pw_type])
953                 goto out;
954
955         l2tp_nl_cmd_ops[pw_type] = ops;
956         ret = 0;
957
958 out:
959         genl_unlock();
960 err:
961         return ret;
962 }
963 EXPORT_SYMBOL_GPL(l2tp_nl_register_ops);
964
965 void l2tp_nl_unregister_ops(enum l2tp_pwtype pw_type)
966 {
967         if (pw_type < __L2TP_PWTYPE_MAX) {
968                 genl_lock();
969                 l2tp_nl_cmd_ops[pw_type] = NULL;
970                 genl_unlock();
971         }
972 }
973 EXPORT_SYMBOL_GPL(l2tp_nl_unregister_ops);
974
975 static int l2tp_nl_init(void)
976 {
977         pr_info("L2TP netlink interface\n");
978         return genl_register_family_with_ops_groups(&l2tp_nl_family,
979                                                     l2tp_nl_ops,
980                                                     l2tp_multicast_group);
981 }
982
983 static void l2tp_nl_cleanup(void)
984 {
985         genl_unregister_family(&l2tp_nl_family);
986 }
987
988 module_init(l2tp_nl_init);
989 module_exit(l2tp_nl_cleanup);
990
991 MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
992 MODULE_DESCRIPTION("L2TP netlink");
993 MODULE_LICENSE("GPL");
994 MODULE_VERSION("1.0");
995 MODULE_ALIAS_GENL_FAMILY("l2tp");