if_gre.c revision 1.173.2.1 1 /* $NetBSD: if_gre.c,v 1.173.2.1 2019/06/10 22:09:45 christos Exp $ */
2
3 /*
4 * Copyright (c) 1998, 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Heiko W.Rupp <hwr (at) pilhuhn.de>
9 *
10 * IPv6-over-GRE contributed by Gert Doering <gert (at) greenie.muc.de>
11 *
12 * GRE over UDP/IPv4/IPv6 sockets contributed by David Young <dyoung (at) NetBSD.org>
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
24 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
27 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
34 *
35 * This material is based upon work partially supported by NSF
36 * under Contract No. NSF CNS-0626584.
37 */
38
39 /*
40 * Encapsulate L3 protocols into IP
41 * See RFC 1701 and 1702 for more details.
42 * If_gre is compatible with Cisco GRE tunnels, so you can
43 * have a NetBSD box as the other end of a tunnel interface of a Cisco
44 * router. See gre(4) for more details.
45 */
46
47 #include <sys/cdefs.h>
48 __KERNEL_RCSID(0, "$NetBSD: if_gre.c,v 1.173.2.1 2019/06/10 22:09:45 christos Exp $");
49
50 #ifdef _KERNEL_OPT
51 #include "opt_atalk.h"
52 #include "opt_gre.h"
53 #include "opt_inet.h"
54 #include "opt_mpls.h"
55 #endif
56
57 #include <sys/param.h>
58 #include <sys/file.h>
59 #include <sys/filedesc.h>
60 #include <sys/malloc.h>
61 #include <sys/mallocvar.h>
62 #include <sys/mbuf.h>
63 #include <sys/proc.h>
64 #include <sys/domain.h>
65 #include <sys/protosw.h>
66 #include <sys/socket.h>
67 #include <sys/socketvar.h>
68 #include <sys/ioctl.h>
69 #include <sys/queue.h>
70 #include <sys/intr.h>
71 #include <sys/systm.h>
72 #include <sys/sysctl.h>
73 #include <sys/kauth.h>
74 #include <sys/device.h>
75 #include <sys/module.h>
76
77 #include <sys/kernel.h>
78 #include <sys/mutex.h>
79 #include <sys/condvar.h>
80 #include <sys/kthread.h>
81
82 #include <sys/cpu.h>
83
84 #include <net/ethertypes.h>
85 #include <net/if.h>
86 #include <net/if_types.h>
87 #include <net/netisr.h>
88 #include <net/route.h>
89 #include <sys/device.h>
90 #include <sys/module.h>
91 #include <sys/atomic.h>
92
93 #include <netinet/in_systm.h>
94 #include <netinet/in.h>
95 #include <netinet/ip.h> /* we always need this for sizeof(struct ip) */
96
97 #ifdef INET
98 #include <netinet/in_var.h>
99 #include <netinet/ip_var.h>
100 #endif
101
102 #ifdef INET6
103 #include <netinet6/in6_var.h>
104 #endif
105
106 #ifdef MPLS
107 #include <netmpls/mpls.h>
108 #include <netmpls/mpls_var.h>
109 #endif
110
111 #ifdef NETATALK
112 #include <netatalk/at.h>
113 #include <netatalk/at_var.h>
114 #include <netatalk/at_extern.h>
115 #endif
116
117 #include <sys/time.h>
118 #include <net/bpf.h>
119
120 #include <net/if_gre.h>
121
122 #include "ioconf.h"
123
124 /*
125 * It is not easy to calculate the right value for a GRE MTU.
126 * We leave this task to the admin and use the same default that
127 * other vendors use.
128 */
129 #define GREMTU 1476
130
131 #ifdef GRE_DEBUG
132 int gre_debug = 0;
133 #define GRE_DPRINTF(__sc, ...) \
134 do { \
135 if (__predict_false(gre_debug || \
136 ((__sc)->sc_if.if_flags & IFF_DEBUG) != 0)) { \
137 printf("%s.%d: ", __func__, __LINE__); \
138 printf(__VA_ARGS__); \
139 } \
140 } while (/*CONSTCOND*/0)
141 #else
142 #define GRE_DPRINTF(__sc, __fmt, ...) do { } while (/*CONSTCOND*/0)
143 #endif /* GRE_DEBUG */
144
145 int ip_gre_ttl = GRE_TTL;
146
147 static u_int gre_count;
148
149 static int gre_clone_create(struct if_clone *, int);
150 static int gre_clone_destroy(struct ifnet *);
151
152 static struct if_clone gre_cloner =
153 IF_CLONE_INITIALIZER("gre", gre_clone_create, gre_clone_destroy);
154
155 static int gre_input(struct gre_softc *, struct mbuf *, const struct gre_h *);
156 static bool gre_is_nullconf(const struct gre_soparm *);
157 static int gre_output(struct ifnet *, struct mbuf *,
158 const struct sockaddr *, const struct rtentry *);
159 static int gre_ioctl(struct ifnet *, u_long, void *);
160 static int gre_getsockname(struct socket *, struct sockaddr *);
161 static int gre_getpeername(struct socket *, struct sockaddr *);
162 static int gre_getnames(struct socket *, struct lwp *,
163 struct sockaddr_storage *, struct sockaddr_storage *);
164 static void gre_clearconf(struct gre_soparm *, bool);
165 static int gre_soreceive(struct socket *, struct mbuf **);
166 static int gre_sosend(struct socket *, struct mbuf *);
167 static struct socket *gre_reconf(struct gre_softc *, const struct gre_soparm *);
168
169 static bool gre_fp_send(struct gre_softc *, enum gre_msg, file_t *);
170 static bool gre_fp_recv(struct gre_softc *);
171 static void gre_fp_recvloop(void *);
172
173 static void
174 gre_bufq_init(struct gre_bufq *bq, size_t len0)
175 {
176 memset(bq, 0, sizeof(*bq));
177 bq->bq_q = pcq_create(len0, KM_SLEEP);
178 KASSERT(bq->bq_q != NULL);
179 }
180
181 static struct mbuf *
182 gre_bufq_dequeue(struct gre_bufq *bq)
183 {
184 return pcq_get(bq->bq_q);
185 }
186
187 static void
188 gre_bufq_purge(struct gre_bufq *bq)
189 {
190 struct mbuf *m;
191
192 while ((m = gre_bufq_dequeue(bq)) != NULL)
193 m_freem(m);
194 }
195
196 static void
197 gre_bufq_destroy(struct gre_bufq *bq)
198 {
199 gre_bufq_purge(bq);
200 pcq_destroy(bq->bq_q);
201 }
202
203 static int
204 gre_bufq_enqueue(struct gre_bufq *bq, struct mbuf *m)
205 {
206 KASSERT(bq->bq_q != NULL);
207
208 if (!pcq_put(bq->bq_q, m)) {
209 bq->bq_drops++;
210 return ENOBUFS;
211 }
212 return 0;
213 }
214
215 static void
216 greintr(void *arg)
217 {
218 struct gre_softc *sc = (struct gre_softc *)arg;
219 struct socket *so = sc->sc_soparm.sp_so;
220 int rc;
221 struct mbuf *m;
222
223 KASSERT(so != NULL);
224
225 sc->sc_send_ev.ev_count++;
226 GRE_DPRINTF(sc, "enter\n");
227 while ((m = gre_bufq_dequeue(&sc->sc_snd)) != NULL) {
228 /* XXX handle ENOBUFS? */
229 if ((rc = gre_sosend(so, m)) != 0)
230 GRE_DPRINTF(sc, "gre_sosend failed %d\n", rc);
231 }
232 }
233
234 /* Caller must hold sc->sc_mtx. */
235 static void
236 gre_fp_wait(struct gre_softc *sc)
237 {
238 sc->sc_fp_waiters++;
239 cv_wait(&sc->sc_fp_condvar, &sc->sc_mtx);
240 sc->sc_fp_waiters--;
241 }
242
243 static void
244 gre_evcnt_detach(struct gre_softc *sc)
245 {
246 evcnt_detach(&sc->sc_recv_ev);
247 evcnt_detach(&sc->sc_block_ev);
248 evcnt_detach(&sc->sc_error_ev);
249 evcnt_detach(&sc->sc_pullup_ev);
250 evcnt_detach(&sc->sc_unsupp_ev);
251
252 evcnt_detach(&sc->sc_send_ev);
253 evcnt_detach(&sc->sc_oflow_ev);
254 }
255
256 static void
257 gre_evcnt_attach(struct gre_softc *sc)
258 {
259 evcnt_attach_dynamic(&sc->sc_recv_ev, EVCNT_TYPE_MISC,
260 NULL, sc->sc_if.if_xname, "recv");
261 evcnt_attach_dynamic(&sc->sc_block_ev, EVCNT_TYPE_MISC,
262 &sc->sc_recv_ev, sc->sc_if.if_xname, "would block");
263 evcnt_attach_dynamic(&sc->sc_error_ev, EVCNT_TYPE_MISC,
264 &sc->sc_recv_ev, sc->sc_if.if_xname, "error");
265 evcnt_attach_dynamic(&sc->sc_pullup_ev, EVCNT_TYPE_MISC,
266 &sc->sc_recv_ev, sc->sc_if.if_xname, "pullup failed");
267 evcnt_attach_dynamic(&sc->sc_unsupp_ev, EVCNT_TYPE_MISC,
268 &sc->sc_recv_ev, sc->sc_if.if_xname, "unsupported");
269
270 evcnt_attach_dynamic(&sc->sc_send_ev, EVCNT_TYPE_MISC,
271 NULL, sc->sc_if.if_xname, "send");
272 evcnt_attach_dynamic(&sc->sc_oflow_ev, EVCNT_TYPE_MISC,
273 &sc->sc_send_ev, sc->sc_if.if_xname, "overflow");
274 }
275
276 static int
277 gre_clone_create(struct if_clone *ifc, int unit)
278 {
279 int rc;
280 struct gre_softc *sc;
281 struct gre_soparm *sp;
282 const struct sockaddr *any;
283
284 if ((any = sockaddr_any_by_family(AF_INET)) == NULL &&
285 (any = sockaddr_any_by_family(AF_INET6)) == NULL)
286 goto fail0;
287
288 sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK|M_ZERO);
289 mutex_init(&sc->sc_mtx, MUTEX_DRIVER, IPL_SOFTNET);
290 cv_init(&sc->sc_condvar, "gre wait");
291 cv_init(&sc->sc_fp_condvar, "gre fp");
292
293 if_initname(&sc->sc_if, ifc->ifc_name, unit);
294 sc->sc_if.if_softc = sc;
295 sc->sc_if.if_type = IFT_TUNNEL;
296 sc->sc_if.if_addrlen = 0;
297 sc->sc_if.if_hdrlen = sizeof(struct ip) + sizeof(struct gre_h);
298 sc->sc_if.if_dlt = DLT_NULL;
299 sc->sc_if.if_mtu = GREMTU;
300 sc->sc_if.if_flags = IFF_POINTOPOINT|IFF_MULTICAST;
301 sc->sc_if.if_output = gre_output;
302 sc->sc_if.if_ioctl = gre_ioctl;
303 sp = &sc->sc_soparm;
304 sockaddr_copy(sstosa(&sp->sp_dst), sizeof(sp->sp_dst), any);
305 sockaddr_copy(sstosa(&sp->sp_src), sizeof(sp->sp_src), any);
306 sp->sp_proto = IPPROTO_GRE;
307 sp->sp_type = SOCK_RAW;
308
309 sc->sc_fd = -1;
310
311 rc = kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL, gre_fp_recvloop, sc,
312 NULL, "%s", sc->sc_if.if_xname);
313 if (rc)
314 goto fail1;
315
316 gre_evcnt_attach(sc);
317
318 gre_bufq_init(&sc->sc_snd, 17);
319 sc->sc_if.if_flags |= IFF_LINK0;
320 if_attach(&sc->sc_if);
321 if_alloc_sadl(&sc->sc_if);
322 bpf_attach(&sc->sc_if, DLT_NULL, sizeof(uint32_t));
323 atomic_inc_uint(&gre_count);
324 return 0;
325
326 fail1:
327 cv_destroy(&sc->sc_fp_condvar);
328 cv_destroy(&sc->sc_condvar);
329 mutex_destroy(&sc->sc_mtx);
330 free(sc, M_DEVBUF);
331
332 fail0:
333 return -1;
334 }
335
336 static int
337 gre_clone_destroy(struct ifnet *ifp)
338 {
339 int s;
340 struct gre_softc *sc = ifp->if_softc;
341
342 GRE_DPRINTF(sc, "\n");
343
344 bpf_detach(ifp);
345 s = splnet();
346 if_detach(ifp);
347
348 GRE_DPRINTF(sc, "\n");
349 /* Note that we must not hold the mutex while we call gre_reconf(). */
350 gre_reconf(sc, NULL);
351
352 mutex_enter(&sc->sc_mtx);
353 sc->sc_msg = GRE_M_STOP;
354 cv_signal(&sc->sc_fp_condvar);
355 while (sc->sc_fp_waiters > 0)
356 cv_wait(&sc->sc_fp_condvar, &sc->sc_mtx);
357 mutex_exit(&sc->sc_mtx);
358
359 splx(s);
360
361 cv_destroy(&sc->sc_condvar);
362 cv_destroy(&sc->sc_fp_condvar);
363 mutex_destroy(&sc->sc_mtx);
364 gre_bufq_destroy(&sc->sc_snd);
365 gre_evcnt_detach(sc);
366 free(sc, M_DEVBUF);
367
368 atomic_dec_uint(&gre_count);
369 return 0;
370 }
371
372 static void
373 gre_receive(struct socket *so, void *arg, int events, int waitflag)
374 {
375 struct gre_softc *sc = (struct gre_softc *)arg;
376 int rc;
377 const struct gre_h *gh;
378 struct mbuf *m;
379
380 GRE_DPRINTF(sc, "enter\n");
381
382 sc->sc_recv_ev.ev_count++;
383
384 rc = gre_soreceive(so, &m);
385 /* TBD Back off if ECONNREFUSED (indicates
386 * ICMP Port Unreachable)?
387 */
388 if (rc == EWOULDBLOCK) {
389 GRE_DPRINTF(sc, "EWOULDBLOCK\n");
390 sc->sc_block_ev.ev_count++;
391 return;
392 } else if (rc != 0 || m == NULL) {
393 GRE_DPRINTF(sc, "%s: rc %d m %p\n",
394 sc->sc_if.if_xname, rc, (void *)m);
395 sc->sc_error_ev.ev_count++;
396 return;
397 }
398 if (m->m_len < sizeof(*gh) && (m = m_pullup(m, sizeof(*gh))) == NULL) {
399 GRE_DPRINTF(sc, "m_pullup failed\n");
400 sc->sc_pullup_ev.ev_count++;
401 return;
402 }
403 gh = mtod(m, const struct gre_h *);
404
405 if (gre_input(sc, m, gh) == 0) {
406 sc->sc_unsupp_ev.ev_count++;
407 GRE_DPRINTF(sc, "dropping unsupported\n");
408 m_freem(m);
409 }
410 }
411
412 static void
413 gre_upcall_add(struct socket *so, void *arg)
414 {
415 /* XXX What if the kernel already set an upcall? */
416 KASSERT((so->so_rcv.sb_flags & SB_UPCALL) == 0);
417 so->so_upcallarg = arg;
418 so->so_upcall = gre_receive;
419 so->so_rcv.sb_flags |= SB_UPCALL;
420 }
421
422 static void
423 gre_upcall_remove(struct socket *so)
424 {
425 so->so_rcv.sb_flags &= ~SB_UPCALL;
426 so->so_upcallarg = NULL;
427 so->so_upcall = NULL;
428 }
429
430 static int
431 gre_socreate(struct gre_softc *sc, const struct gre_soparm *sp, int *fdout)
432 {
433 int fd, rc;
434 struct socket *so;
435 struct sockaddr_big sbig;
436 sa_family_t af;
437 int val;
438
439 GRE_DPRINTF(sc, "enter\n");
440
441 af = sp->sp_src.ss_family;
442 rc = fsocreate(af, NULL, sp->sp_type, sp->sp_proto, &fd);
443 if (rc != 0) {
444 GRE_DPRINTF(sc, "fsocreate failed\n");
445 return rc;
446 }
447
448 if ((rc = fd_getsock(fd, &so)) != 0)
449 return rc;
450
451 memcpy(&sbig, &sp->sp_src, sizeof(sp->sp_src));
452 if ((rc = sobind(so, (struct sockaddr *)&sbig, curlwp)) != 0) {
453 GRE_DPRINTF(sc, "sobind failed\n");
454 goto out;
455 }
456
457 memcpy(&sbig, &sp->sp_dst, sizeof(sp->sp_dst));
458 solock(so);
459 if ((rc = soconnect(so, (struct sockaddr *)&sbig, curlwp)) != 0) {
460 GRE_DPRINTF(sc, "soconnect failed\n");
461 sounlock(so);
462 goto out;
463 }
464 sounlock(so);
465
466 /* XXX convert to a (new) SOL_SOCKET call */
467 KASSERT(so->so_proto != NULL);
468 rc = so_setsockopt(curlwp, so, IPPROTO_IP, IP_TTL,
469 &ip_gre_ttl, sizeof(ip_gre_ttl));
470 if (rc != 0) {
471 GRE_DPRINTF(sc, "so_setsockopt ttl failed\n");
472 rc = 0;
473 }
474
475 val = 1;
476 rc = so_setsockopt(curlwp, so, SOL_SOCKET, SO_NOHEADER,
477 &val, sizeof(val));
478 if (rc != 0) {
479 GRE_DPRINTF(sc, "so_setsockopt SO_NOHEADER failed\n");
480 rc = 0;
481 }
482 out:
483 if (rc != 0)
484 fd_close(fd);
485 else {
486 fd_putfile(fd);
487 *fdout = fd;
488 }
489
490 return rc;
491 }
492
493 static int
494 gre_sosend(struct socket *so, struct mbuf *top)
495 {
496 struct proc *p;
497 long space, resid;
498 int error;
499 struct lwp * const l = curlwp;
500
501 p = l->l_proc;
502
503 resid = top->m_pkthdr.len;
504 if (p)
505 l->l_ru.ru_msgsnd++;
506 #define snderr(errno) { error = errno; goto release; }
507
508 solock(so);
509 if ((error = sblock(&so->so_snd, M_NOWAIT)) != 0)
510 goto out;
511 if (so->so_state & SS_CANTSENDMORE)
512 snderr(EPIPE);
513 if (so->so_error) {
514 error = so->so_error;
515 so->so_error = 0;
516 goto release;
517 }
518 if ((so->so_state & SS_ISCONNECTED) == 0) {
519 if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
520 snderr(ENOTCONN);
521 } else {
522 snderr(EDESTADDRREQ);
523 }
524 }
525 space = sbspace(&so->so_snd);
526 if (resid > so->so_snd.sb_hiwat)
527 snderr(EMSGSIZE);
528 if (space < resid)
529 snderr(EWOULDBLOCK);
530 /*
531 * Data is prepackaged in "top".
532 */
533 if (so->so_state & SS_CANTSENDMORE)
534 snderr(EPIPE);
535 error = (*so->so_proto->pr_usrreqs->pr_send)(so,
536 top, NULL, NULL, l);
537 top = NULL;
538 release:
539 sbunlock(&so->so_snd);
540 out:
541 sounlock(so);
542 if (top != NULL)
543 m_freem(top);
544 return error;
545 }
546
547 /* This is a stripped-down version of soreceive() that will never
548 * block. It will support SOCK_DGRAM sockets. It may also support
549 * SOCK_SEQPACKET sockets.
550 */
551 static int
552 gre_soreceive(struct socket *so, struct mbuf **mp0)
553 {
554 struct mbuf *m, **mp;
555 int flags, len, error, type;
556 const struct protosw *pr;
557 struct mbuf *nextrecord;
558
559 KASSERT(mp0 != NULL);
560
561 flags = MSG_DONTWAIT;
562 pr = so->so_proto;
563 mp = mp0;
564 type = 0;
565
566 *mp = NULL;
567
568 KASSERT(pr->pr_flags & PR_ATOMIC);
569 restart:
570 if ((error = sblock(&so->so_rcv, M_NOWAIT)) != 0) {
571 return error;
572 }
573 m = so->so_rcv.sb_mb;
574 /*
575 * If we have less data than requested, do not block awaiting more.
576 */
577 if (m == NULL) {
578 #ifdef DIAGNOSTIC
579 if (so->so_rcv.sb_cc)
580 panic("receive 1");
581 #endif
582 if (so->so_error) {
583 error = so->so_error;
584 so->so_error = 0;
585 } else if (so->so_state & SS_CANTRCVMORE)
586 ;
587 else if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0
588 && (so->so_proto->pr_flags & PR_CONNREQUIRED))
589 error = ENOTCONN;
590 else
591 error = EWOULDBLOCK;
592 goto release;
593 }
594 /*
595 * On entry here, m points to the first record of the socket buffer.
596 * While we process the initial mbufs containing address and control
597 * info, we save a copy of m->m_nextpkt into nextrecord.
598 */
599 if (curlwp != NULL)
600 curlwp->l_ru.ru_msgrcv++;
601 KASSERT(m == so->so_rcv.sb_mb);
602 SBLASTRECORDCHK(&so->so_rcv, "soreceive 1");
603 SBLASTMBUFCHK(&so->so_rcv, "soreceive 1");
604 nextrecord = m->m_nextpkt;
605 if (pr->pr_flags & PR_ADDR) {
606 #ifdef DIAGNOSTIC
607 if (m->m_type != MT_SONAME)
608 panic("receive 1a");
609 #endif
610 sbfree(&so->so_rcv, m);
611 m = so->so_rcv.sb_mb = m_free(m);
612 }
613 while (m != NULL && m->m_type == MT_CONTROL && error == 0) {
614 sbfree(&so->so_rcv, m);
615 /*
616 * Dispose of any SCM_RIGHTS message that went
617 * through the read path rather than recv.
618 */
619 if (pr->pr_domain->dom_dispose &&
620 mtod(m, struct cmsghdr *)->cmsg_type == SCM_RIGHTS)
621 (*pr->pr_domain->dom_dispose)(m);
622 m = so->so_rcv.sb_mb = m_free(m);
623 }
624
625 /*
626 * If m is non-NULL, we have some data to read. From now on,
627 * make sure to keep sb_lastrecord consistent when working on
628 * the last packet on the chain (nextrecord == NULL) and we
629 * change m->m_nextpkt.
630 */
631 if (m != NULL) {
632 m->m_nextpkt = nextrecord;
633 /*
634 * If nextrecord == NULL (this is a single chain),
635 * then sb_lastrecord may not be valid here if m
636 * was changed earlier.
637 */
638 if (nextrecord == NULL) {
639 KASSERT(so->so_rcv.sb_mb == m);
640 so->so_rcv.sb_lastrecord = m;
641 }
642 type = m->m_type;
643 if (type == MT_OOBDATA)
644 flags |= MSG_OOB;
645 } else {
646 KASSERT(so->so_rcv.sb_mb == m);
647 so->so_rcv.sb_mb = nextrecord;
648 SB_EMPTY_FIXUP(&so->so_rcv);
649 }
650 SBLASTRECORDCHK(&so->so_rcv, "soreceive 2");
651 SBLASTMBUFCHK(&so->so_rcv, "soreceive 2");
652
653 while (m != NULL) {
654 if (m->m_type == MT_OOBDATA) {
655 if (type != MT_OOBDATA)
656 break;
657 } else if (type == MT_OOBDATA)
658 break;
659 #ifdef DIAGNOSTIC
660 else if (m->m_type != MT_DATA && m->m_type != MT_HEADER)
661 panic("receive 3");
662 #endif
663 so->so_state &= ~SS_RCVATMARK;
664 if (so->so_oobmark != 0 && so->so_oobmark < m->m_len)
665 break;
666 len = m->m_len;
667 /*
668 * mp is set, just pass back the mbufs.
669 * Sockbuf must be consistent here (points to current mbuf,
670 * it points to next record) when we drop priority;
671 * we must note any additions to the sockbuf when we
672 * block interrupts again.
673 */
674 if (m->m_flags & M_EOR)
675 flags |= MSG_EOR;
676 nextrecord = m->m_nextpkt;
677 sbfree(&so->so_rcv, m);
678 *mp = m;
679 mp = &m->m_next;
680 so->so_rcv.sb_mb = m = m->m_next;
681 *mp = NULL;
682 /*
683 * If m != NULL, we also know that
684 * so->so_rcv.sb_mb != NULL.
685 */
686 KASSERT(so->so_rcv.sb_mb == m);
687 if (m) {
688 m->m_nextpkt = nextrecord;
689 if (nextrecord == NULL)
690 so->so_rcv.sb_lastrecord = m;
691 } else {
692 so->so_rcv.sb_mb = nextrecord;
693 SB_EMPTY_FIXUP(&so->so_rcv);
694 }
695 SBLASTRECORDCHK(&so->so_rcv, "soreceive 3");
696 SBLASTMBUFCHK(&so->so_rcv, "soreceive 3");
697 if (so->so_oobmark) {
698 so->so_oobmark -= len;
699 if (so->so_oobmark == 0) {
700 so->so_state |= SS_RCVATMARK;
701 break;
702 }
703 }
704 if (flags & MSG_EOR)
705 break;
706 }
707
708 if (m != NULL) {
709 m_freem(*mp);
710 *mp = NULL;
711 error = ENOMEM;
712 (void) sbdroprecord(&so->so_rcv);
713 } else {
714 /*
715 * First part is an inline SB_EMPTY_FIXUP(). Second
716 * part makes sure sb_lastrecord is up-to-date if
717 * there is still data in the socket buffer.
718 */
719 so->so_rcv.sb_mb = nextrecord;
720 if (so->so_rcv.sb_mb == NULL) {
721 so->so_rcv.sb_mbtail = NULL;
722 so->so_rcv.sb_lastrecord = NULL;
723 } else if (nextrecord->m_nextpkt == NULL)
724 so->so_rcv.sb_lastrecord = nextrecord;
725 }
726 SBLASTRECORDCHK(&so->so_rcv, "soreceive 4");
727 SBLASTMBUFCHK(&so->so_rcv, "soreceive 4");
728 if (pr->pr_flags & PR_WANTRCVD && so->so_pcb)
729 (*pr->pr_usrreqs->pr_rcvd)(so, flags, curlwp);
730 if (*mp0 == NULL && (flags & MSG_EOR) == 0 &&
731 (so->so_state & SS_CANTRCVMORE) == 0) {
732 sbunlock(&so->so_rcv);
733 goto restart;
734 }
735
736 release:
737 sbunlock(&so->so_rcv);
738 return error;
739 }
740
741 static struct socket *
742 gre_reconf(struct gre_softc *sc, const struct gre_soparm *newsoparm)
743 {
744 struct ifnet *ifp = &sc->sc_if;
745
746 GRE_DPRINTF(sc, "enter\n");
747
748 shutdown:
749 if (sc->sc_soparm.sp_so != NULL) {
750 GRE_DPRINTF(sc, "\n");
751 gre_upcall_remove(sc->sc_soparm.sp_so);
752 softint_disestablish(sc->sc_si);
753 sc->sc_si = NULL;
754 gre_fp_send(sc, GRE_M_DELFP, NULL);
755 gre_clearconf(&sc->sc_soparm, false);
756 }
757
758 if (newsoparm != NULL) {
759 GRE_DPRINTF(sc, "\n");
760 sc->sc_soparm = *newsoparm;
761 newsoparm = NULL;
762 }
763
764 if (sc->sc_soparm.sp_so != NULL) {
765 GRE_DPRINTF(sc, "\n");
766 sc->sc_si = softint_establish(SOFTINT_NET, greintr, sc);
767 gre_upcall_add(sc->sc_soparm.sp_so, sc);
768 if ((ifp->if_flags & IFF_UP) == 0) {
769 GRE_DPRINTF(sc, "down\n");
770 goto shutdown;
771 }
772 }
773
774 GRE_DPRINTF(sc, "\n");
775 if (sc->sc_soparm.sp_so != NULL)
776 sc->sc_if.if_flags |= IFF_RUNNING;
777 else {
778 gre_bufq_purge(&sc->sc_snd);
779 sc->sc_if.if_flags &= ~IFF_RUNNING;
780 }
781 return sc->sc_soparm.sp_so;
782 }
783
784 static int
785 gre_input(struct gre_softc *sc, struct mbuf *m, const struct gre_h *gh)
786 {
787 pktqueue_t *pktq = NULL;
788 struct ifqueue *ifq = NULL;
789 uint16_t flags;
790 uint32_t af; /* af passed to BPF tap */
791 int isr = 0, s;
792 int hlen;
793
794 sc->sc_if.if_ipackets++;
795 sc->sc_if.if_ibytes += m->m_pkthdr.len;
796
797 hlen = sizeof(struct gre_h);
798
799 /* process GRE flags as packet can be of variable len */
800 flags = ntohs(gh->flags);
801
802 /* Checksum & Offset are present */
803 if ((flags & GRE_CP) | (flags & GRE_RP))
804 hlen += 4;
805 /* We don't support routing fields (variable length) */
806 if (flags & GRE_RP) {
807 sc->sc_if.if_ierrors++;
808 return 0;
809 }
810 if (flags & GRE_KP)
811 hlen += 4;
812 if (flags & GRE_SP)
813 hlen += 4;
814
815 switch (ntohs(gh->ptype)) { /* ethertypes */
816 #ifdef INET
817 case ETHERTYPE_IP:
818 pktq = ip_pktq;
819 af = AF_INET;
820 break;
821 #endif
822 #ifdef NETATALK
823 case ETHERTYPE_ATALK:
824 ifq = &atintrq1;
825 isr = NETISR_ATALK;
826 af = AF_APPLETALK;
827 break;
828 #endif
829 #ifdef INET6
830 case ETHERTYPE_IPV6:
831 pktq = ip6_pktq;
832 af = AF_INET6;
833 break;
834 #endif
835 #ifdef MPLS
836 case ETHERTYPE_MPLS:
837 ifq = &mplsintrq;
838 isr = NETISR_MPLS;
839 af = AF_MPLS;
840 break;
841 #endif
842 default: /* others not yet supported */
843 GRE_DPRINTF(sc, "unhandled ethertype 0x%04x\n",
844 ntohs(gh->ptype));
845 sc->sc_if.if_noproto++;
846 return 0;
847 }
848
849 if (hlen > m->m_pkthdr.len) {
850 m_freem(m);
851 sc->sc_if.if_ierrors++;
852 return 1;
853 }
854 m_adj(m, hlen);
855
856 bpf_mtap_af(&sc->sc_if, af, m, BPF_D_IN);
857
858 m_set_rcvif(m, &sc->sc_if);
859
860 if (__predict_true(pktq)) {
861 if (__predict_false(!pktq_enqueue(pktq, m, 0))) {
862 m_freem(m);
863 }
864 return 1;
865 }
866
867 s = splnet();
868 if (IF_QFULL(ifq)) {
869 IF_DROP(ifq);
870 m_freem(m);
871 } else {
872 IF_ENQUEUE(ifq, m);
873 }
874 /* we need schednetisr since the address family may change */
875 schednetisr(isr);
876 splx(s);
877
878 return 1; /* packet is done, no further processing needed */
879 }
880
881 /*
882 * The output routine. Takes a packet and encapsulates it in the protocol
883 * given by sc->sc_soparm.sp_proto. See also RFC 1701 and RFC 2004
884 */
885 static int
886 gre_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
887 const struct rtentry *rt)
888 {
889 int error = 0;
890 struct gre_softc *sc = ifp->if_softc;
891 struct gre_h *gh;
892 uint16_t etype = 0;
893
894 KASSERT((m->m_flags & M_PKTHDR) != 0);
895
896 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) {
897 m_freem(m);
898 error = ENETDOWN;
899 goto end;
900 }
901
902 bpf_mtap_af(ifp, dst->sa_family, m, BPF_D_OUT);
903
904 m->m_flags &= ~(M_BCAST|M_MCAST);
905
906 GRE_DPRINTF(sc, "dst->sa_family=%d\n", dst->sa_family);
907 switch (dst->sa_family) {
908 #ifdef INET
909 case AF_INET:
910 /*
911 * TBD Extract the IP ToS field and set the
912 * encapsulating protocol's ToS to suit.
913 */
914 etype = htons(ETHERTYPE_IP);
915 break;
916 #endif
917 #ifdef NETATALK
918 case AF_APPLETALK:
919 etype = htons(ETHERTYPE_ATALK);
920 break;
921 #endif
922 #ifdef INET6
923 case AF_INET6:
924 etype = htons(ETHERTYPE_IPV6);
925 break;
926 #endif
927 default:
928 IF_DROP(&ifp->if_snd);
929 m_freem(m);
930 error = EAFNOSUPPORT;
931 goto end;
932 }
933
934 #ifdef MPLS
935 if (rt != NULL && rt_gettag(rt) != NULL) {
936 union mpls_shim msh;
937 msh.s_addr = MPLS_GETSADDR(rt);
938 if (msh.shim.label != MPLS_LABEL_IMPLNULL)
939 etype = htons(ETHERTYPE_MPLS);
940 }
941 #endif
942
943 M_PREPEND(m, sizeof(*gh), M_DONTWAIT);
944
945 if (m == NULL) {
946 IF_DROP(&ifp->if_snd);
947 error = ENOBUFS;
948 goto end;
949 }
950
951 gh = mtod(m, struct gre_h *);
952 gh->flags = 0;
953 gh->ptype = etype;
954 /* XXX Need to handle IP ToS. Look at how I handle IP TTL. */
955
956 ifp->if_opackets++;
957 ifp->if_obytes += m->m_pkthdr.len;
958
959 /* Clear checksum-offload flags. */
960 m->m_pkthdr.csum_flags = 0;
961 m->m_pkthdr.csum_data = 0;
962
963 /* send it off */
964 if ((error = gre_bufq_enqueue(&sc->sc_snd, m)) != 0) {
965 sc->sc_oflow_ev.ev_count++;
966 m_freem(m);
967 } else
968 softint_schedule(sc->sc_si);
969
970 end:
971 if (error)
972 ifp->if_oerrors++;
973 return error;
974 }
975
976 static int
977 gre_getsockname(struct socket *so, struct sockaddr *nam)
978 {
979 return (*so->so_proto->pr_usrreqs->pr_sockaddr)(so, nam);
980 }
981
982 static int
983 gre_getpeername(struct socket *so, struct sockaddr *nam)
984 {
985 return (*so->so_proto->pr_usrreqs->pr_peeraddr)(so, nam);
986 }
987
988 static int
989 gre_getnames(struct socket *so, struct lwp *l, struct sockaddr_storage *src,
990 struct sockaddr_storage *dst)
991 {
992 struct sockaddr_storage ss;
993 int rc;
994
995 solock(so);
996 if ((rc = gre_getsockname(so, (struct sockaddr *)&ss)) != 0)
997 goto out;
998 *src = ss;
999
1000 if ((rc = gre_getpeername(so, (struct sockaddr *)&ss)) != 0)
1001 goto out;
1002 *dst = ss;
1003 out:
1004 sounlock(so);
1005 return rc;
1006 }
1007
1008 static void
1009 gre_fp_recvloop(void *arg)
1010 {
1011 struct gre_softc *sc = arg;
1012
1013 mutex_enter(&sc->sc_mtx);
1014 while (gre_fp_recv(sc))
1015 ;
1016 mutex_exit(&sc->sc_mtx);
1017 kthread_exit(0);
1018 }
1019
1020 static bool
1021 gre_fp_recv(struct gre_softc *sc)
1022 {
1023 int fd, ofd, rc;
1024 file_t *fp;
1025
1026 fp = sc->sc_fp;
1027 ofd = sc->sc_fd;
1028 fd = -1;
1029
1030 switch (sc->sc_msg) {
1031 case GRE_M_STOP:
1032 cv_signal(&sc->sc_fp_condvar);
1033 return false;
1034 case GRE_M_SETFP:
1035 mutex_exit(&sc->sc_mtx);
1036 rc = fd_dup(fp, 0, &fd, 0);
1037 mutex_enter(&sc->sc_mtx);
1038 if (rc != 0) {
1039 sc->sc_msg = GRE_M_ERR;
1040 break;
1041 }
1042 /*FALLTHROUGH*/
1043 case GRE_M_DELFP:
1044 mutex_exit(&sc->sc_mtx);
1045 if (ofd != -1 && fd_getfile(ofd) != NULL)
1046 fd_close(ofd);
1047 mutex_enter(&sc->sc_mtx);
1048 sc->sc_fd = fd;
1049 sc->sc_msg = GRE_M_OK;
1050 break;
1051 default:
1052 gre_fp_wait(sc);
1053 return true;
1054 }
1055 cv_signal(&sc->sc_fp_condvar);
1056 return true;
1057 }
1058
1059 static bool
1060 gre_fp_send(struct gre_softc *sc, enum gre_msg msg, file_t *fp)
1061 {
1062 bool rc;
1063
1064 mutex_enter(&sc->sc_mtx);
1065 while (sc->sc_msg != GRE_M_NONE)
1066 gre_fp_wait(sc);
1067 sc->sc_fp = fp;
1068 sc->sc_msg = msg;
1069 cv_signal(&sc->sc_fp_condvar);
1070 while (sc->sc_msg != GRE_M_STOP && sc->sc_msg != GRE_M_OK &&
1071 sc->sc_msg != GRE_M_ERR)
1072 gre_fp_wait(sc);
1073 rc = (sc->sc_msg != GRE_M_ERR);
1074 sc->sc_msg = GRE_M_NONE;
1075 cv_signal(&sc->sc_fp_condvar);
1076 mutex_exit(&sc->sc_mtx);
1077 return rc;
1078 }
1079
1080 static int
1081 gre_ssock(struct ifnet *ifp, struct gre_soparm *sp, int fd)
1082 {
1083 int error = 0;
1084 const struct protosw *pr;
1085 file_t *fp;
1086 struct gre_softc *sc = ifp->if_softc;
1087 struct socket *so;
1088 struct sockaddr_storage dst, src;
1089
1090 if ((fp = fd_getfile(fd)) == NULL)
1091 return EBADF;
1092 if (fp->f_type != DTYPE_SOCKET) {
1093 fd_putfile(fd);
1094 return ENOTSOCK;
1095 }
1096
1097 GRE_DPRINTF(sc, "\n");
1098
1099 so = fp->f_socket;
1100 pr = so->so_proto;
1101
1102 GRE_DPRINTF(sc, "type %d, proto %d\n", pr->pr_type, pr->pr_protocol);
1103
1104 if ((pr->pr_flags & PR_ATOMIC) == 0 ||
1105 (sp->sp_type != 0 && pr->pr_type != sp->sp_type) ||
1106 (sp->sp_proto != 0 && pr->pr_protocol != 0 &&
1107 pr->pr_protocol != sp->sp_proto)) {
1108 error = EINVAL;
1109 goto err;
1110 }
1111
1112 GRE_DPRINTF(sc, "\n");
1113
1114 /* check address */
1115 if ((error = gre_getnames(so, curlwp, &src, &dst)) != 0)
1116 goto err;
1117
1118 GRE_DPRINTF(sc, "\n");
1119
1120 if (!gre_fp_send(sc, GRE_M_SETFP, fp)) {
1121 error = EBUSY;
1122 goto err;
1123 }
1124
1125 GRE_DPRINTF(sc, "\n");
1126
1127 sp->sp_src = src;
1128 sp->sp_dst = dst;
1129
1130 sp->sp_so = so;
1131
1132 err:
1133 fd_putfile(fd);
1134 return error;
1135 }
1136
1137 static bool
1138 sockaddr_is_anyaddr(const struct sockaddr *sa)
1139 {
1140 socklen_t anylen, salen;
1141 const void *anyaddr, *addr;
1142
1143 if ((anyaddr = sockaddr_anyaddr(sa, &anylen)) == NULL ||
1144 (addr = sockaddr_const_addr(sa, &salen)) == NULL)
1145 return false;
1146
1147 if (salen > anylen)
1148 return false;
1149
1150 return memcmp(anyaddr, addr, MIN(anylen, salen)) == 0;
1151 }
1152
1153 static bool
1154 gre_is_nullconf(const struct gre_soparm *sp)
1155 {
1156 return sockaddr_is_anyaddr(sstocsa(&sp->sp_src)) ||
1157 sockaddr_is_anyaddr(sstocsa(&sp->sp_dst));
1158 }
1159
1160 static void
1161 gre_clearconf(struct gre_soparm *sp, bool force)
1162 {
1163 if (sp->sp_bysock || force) {
1164 sockaddr_copy(sstosa(&sp->sp_src), sizeof(sp->sp_src),
1165 sockaddr_any(sstosa(&sp->sp_src)));
1166 sockaddr_copy(sstosa(&sp->sp_dst), sizeof(sp->sp_dst),
1167 sockaddr_any(sstosa(&sp->sp_dst)));
1168 sp->sp_bysock = false;
1169 }
1170 sp->sp_so = NULL; /* XXX */
1171 }
1172
1173 static int
1174 gre_ioctl(struct ifnet *ifp, const u_long cmd, void *data)
1175 {
1176 struct ifreq *ifr;
1177 struct ifaddr *ifa = (struct ifaddr *)data;
1178 struct if_laddrreq *lifr = (struct if_laddrreq *)data;
1179 struct gre_softc *sc = ifp->if_softc;
1180 struct gre_soparm *sp;
1181 int fd, error = 0, oproto, otype, s;
1182 struct gre_soparm sp0;
1183
1184 ifr = data;
1185
1186 GRE_DPRINTF(sc, "cmd %lu\n", cmd);
1187
1188 switch (cmd) {
1189 case GRESPROTO:
1190 case GRESADDRD:
1191 case GRESADDRS:
1192 case GRESSOCK:
1193 case GREDSOCK:
1194 if (kauth_authorize_network(curlwp->l_cred,
1195 KAUTH_NETWORK_INTERFACE,
1196 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd,
1197 NULL) != 0)
1198 return EPERM;
1199 break;
1200 default:
1201 break;
1202 }
1203
1204 s = splnet();
1205
1206 sp0 = sc->sc_soparm;
1207 sp0.sp_so = NULL;
1208 sp = &sp0;
1209
1210 GRE_DPRINTF(sc, "\n");
1211
1212 switch (cmd) {
1213 case SIOCINITIFADDR:
1214 GRE_DPRINTF(sc, "\n");
1215 if ((ifp->if_flags & IFF_UP) != 0)
1216 break;
1217 gre_clearconf(sp, false);
1218 ifp->if_flags |= IFF_UP;
1219 ifa->ifa_rtrequest = p2p_rtrequest;
1220 goto mksocket;
1221 case SIOCSIFFLAGS:
1222 if ((error = ifioctl_common(ifp, cmd, data)) != 0)
1223 break;
1224 oproto = sp->sp_proto;
1225 otype = sp->sp_type;
1226 switch (ifr->ifr_flags & (IFF_LINK0|IFF_LINK2)) {
1227 case IFF_LINK0|IFF_LINK2:
1228 sp->sp_proto = IPPROTO_UDP;
1229 sp->sp_type = SOCK_DGRAM;
1230 break;
1231 case IFF_LINK2:
1232 sp->sp_proto = 0;
1233 sp->sp_type = 0;
1234 break;
1235 case IFF_LINK0:
1236 sp->sp_proto = IPPROTO_GRE;
1237 sp->sp_type = SOCK_RAW;
1238 break;
1239 default:
1240 GRE_DPRINTF(sc, "\n");
1241 error = EINVAL;
1242 goto out;
1243 }
1244 GRE_DPRINTF(sc, "\n");
1245 gre_clearconf(sp, false);
1246 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) ==
1247 (IFF_UP|IFF_RUNNING) &&
1248 (oproto == sp->sp_proto || sp->sp_proto == 0) &&
1249 (otype == sp->sp_type || sp->sp_type == 0))
1250 break;
1251 switch (sp->sp_proto) {
1252 case IPPROTO_UDP:
1253 case IPPROTO_GRE:
1254 goto mksocket;
1255 default:
1256 break;
1257 }
1258 break;
1259 case SIOCSIFMTU:
1260 /* XXX determine MTU automatically by probing w/
1261 * XXX do-not-fragment packets?
1262 */
1263 if (ifr->ifr_mtu < 576) {
1264 error = EINVAL;
1265 break;
1266 }
1267 /*FALLTHROUGH*/
1268 case SIOCGIFMTU:
1269 if ((error = ifioctl_common(ifp, cmd, data)) == ENETRESET)
1270 error = 0;
1271 break;
1272 case SIOCADDMULTI:
1273 case SIOCDELMULTI:
1274 if (ifr == NULL) {
1275 error = EAFNOSUPPORT;
1276 break;
1277 }
1278 switch (ifreq_getaddr(cmd, ifr)->sa_family) {
1279 #ifdef INET
1280 case AF_INET:
1281 break;
1282 #endif
1283 #ifdef INET6
1284 case AF_INET6:
1285 break;
1286 #endif
1287 default:
1288 error = EAFNOSUPPORT;
1289 break;
1290 }
1291 break;
1292 case GRESPROTO:
1293 gre_clearconf(sp, false);
1294 oproto = sp->sp_proto;
1295 otype = sp->sp_type;
1296 sp->sp_proto = ifr->ifr_flags;
1297 switch (sp->sp_proto) {
1298 case IPPROTO_UDP:
1299 ifp->if_flags |= IFF_LINK0|IFF_LINK2;
1300 sp->sp_type = SOCK_DGRAM;
1301 break;
1302 case IPPROTO_GRE:
1303 ifp->if_flags |= IFF_LINK0;
1304 ifp->if_flags &= ~IFF_LINK2;
1305 sp->sp_type = SOCK_RAW;
1306 break;
1307 case 0:
1308 ifp->if_flags &= ~IFF_LINK0;
1309 ifp->if_flags |= IFF_LINK2;
1310 sp->sp_type = 0;
1311 break;
1312 default:
1313 error = EPROTONOSUPPORT;
1314 break;
1315 }
1316 if ((oproto == sp->sp_proto || sp->sp_proto == 0) &&
1317 (otype == sp->sp_type || sp->sp_type == 0))
1318 break;
1319 switch (sp->sp_proto) {
1320 case IPPROTO_UDP:
1321 case IPPROTO_GRE:
1322 goto mksocket;
1323 default:
1324 break;
1325 }
1326 break;
1327 case GREGPROTO:
1328 ifr->ifr_flags = sp->sp_proto;
1329 break;
1330 case GRESADDRS:
1331 case GRESADDRD:
1332 gre_clearconf(sp, false);
1333 /* set tunnel endpoints and mark interface as up */
1334 switch (cmd) {
1335 case GRESADDRS:
1336 sockaddr_copy(sstosa(&sp->sp_src),
1337 sizeof(sp->sp_src), ifreq_getaddr(cmd, ifr));
1338 break;
1339 case GRESADDRD:
1340 sockaddr_copy(sstosa(&sp->sp_dst),
1341 sizeof(sp->sp_dst), ifreq_getaddr(cmd, ifr));
1342 break;
1343 }
1344 checkaddr:
1345 if (sockaddr_any(sstosa(&sp->sp_src)) == NULL ||
1346 sockaddr_any(sstosa(&sp->sp_dst)) == NULL) {
1347 error = EINVAL;
1348 break;
1349 }
1350 /* let gre_socreate() check the rest */
1351 mksocket:
1352 GRE_DPRINTF(sc, "\n");
1353 /* If we're administratively down, or the configuration
1354 * is empty, there's no use creating a socket.
1355 */
1356 if ((ifp->if_flags & IFF_UP) == 0 || gre_is_nullconf(sp))
1357 goto sendconf;
1358
1359 GRE_DPRINTF(sc, "\n");
1360 fd = 0;
1361 error = gre_socreate(sc, sp, &fd);
1362 if (error != 0)
1363 break;
1364
1365 setsock:
1366 GRE_DPRINTF(sc, "\n");
1367
1368 error = gre_ssock(ifp, sp, fd);
1369
1370 if (cmd != GRESSOCK) {
1371 GRE_DPRINTF(sc, "\n");
1372 /* XXX v. dodgy */
1373 if (fd_getfile(fd) != NULL)
1374 fd_close(fd);
1375 }
1376
1377 if (error == 0) {
1378 sendconf:
1379 GRE_DPRINTF(sc, "\n");
1380 ifp->if_flags &= ~IFF_RUNNING;
1381 gre_reconf(sc, sp);
1382 }
1383
1384 break;
1385 case GREGADDRS:
1386 ifreq_setaddr(cmd, ifr, sstosa(&sp->sp_src));
1387 break;
1388 case GREGADDRD:
1389 ifreq_setaddr(cmd, ifr, sstosa(&sp->sp_dst));
1390 break;
1391 case GREDSOCK:
1392 GRE_DPRINTF(sc, "\n");
1393 if (sp->sp_bysock)
1394 ifp->if_flags &= ~IFF_UP;
1395 gre_clearconf(sp, false);
1396 goto mksocket;
1397 case GRESSOCK:
1398 GRE_DPRINTF(sc, "\n");
1399 gre_clearconf(sp, true);
1400 fd = (int)ifr->ifr_value;
1401 sp->sp_bysock = true;
1402 ifp->if_flags |= IFF_UP;
1403 goto setsock;
1404 case SIOCSLIFPHYADDR:
1405 GRE_DPRINTF(sc, "\n");
1406 if (lifr->addr.ss_family != lifr->dstaddr.ss_family) {
1407 error = EAFNOSUPPORT;
1408 break;
1409 }
1410 sockaddr_copy(sstosa(&sp->sp_src), sizeof(sp->sp_src),
1411 sstosa(&lifr->addr));
1412 sockaddr_copy(sstosa(&sp->sp_dst), sizeof(sp->sp_dst),
1413 sstosa(&lifr->dstaddr));
1414 GRE_DPRINTF(sc, "\n");
1415 goto checkaddr;
1416 case SIOCDIFPHYADDR:
1417 GRE_DPRINTF(sc, "\n");
1418 gre_clearconf(sp, true);
1419 ifp->if_flags &= ~IFF_UP;
1420 goto mksocket;
1421 case SIOCGLIFPHYADDR:
1422 GRE_DPRINTF(sc, "\n");
1423 if (gre_is_nullconf(sp)) {
1424 error = EADDRNOTAVAIL;
1425 break;
1426 }
1427 sockaddr_copy(sstosa(&lifr->addr), sizeof(lifr->addr),
1428 sstosa(&sp->sp_src));
1429 sockaddr_copy(sstosa(&lifr->dstaddr), sizeof(lifr->dstaddr),
1430 sstosa(&sp->sp_dst));
1431 GRE_DPRINTF(sc, "\n");
1432 break;
1433 default:
1434 error = ifioctl_common(ifp, cmd, data);
1435 break;
1436 }
1437 out:
1438 GRE_DPRINTF(sc, "\n");
1439 splx(s);
1440 return error;
1441 }
1442
1443 /* ARGSUSED */
1444 void
1445 greattach(int count)
1446 {
1447
1448 /*
1449 * Nothing to do here, initialization is handled by the
1450 * module initialization code in greinit() below.
1451 */
1452 }
1453
1454 static void
1455 greinit(void)
1456 {
1457 if_clone_attach(&gre_cloner);
1458 }
1459
1460 static int
1461 gredetach(void)
1462 {
1463 int error = 0;
1464
1465 if (gre_count != 0)
1466 error = EBUSY;
1467
1468 if (error == 0)
1469 if_clone_detach(&gre_cloner);
1470
1471 return error;
1472 }
1473
1474 /*
1475 * Module infrastructure
1476 */
1477 #include "if_module.h"
1478
1479 IF_MODULE(MODULE_CLASS_DRIVER, gre, NULL)
1480