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