if_gre.c revision 1.175.2.1 1 /* $NetBSD: if_gre.c,v 1.175.2.1 2019/11/01 09:34:27 martin 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.175.2.1 2019/11/01 09:34:27 martin 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 kpreempt_disable();
969 softint_schedule(sc->sc_si);
970 kpreempt_enable();
971 }
972
973 end:
974 if (error)
975 ifp->if_oerrors++;
976 return error;
977 }
978
979 static int
980 gre_getsockname(struct socket *so, struct sockaddr *nam)
981 {
982 return (*so->so_proto->pr_usrreqs->pr_sockaddr)(so, nam);
983 }
984
985 static int
986 gre_getpeername(struct socket *so, struct sockaddr *nam)
987 {
988 return (*so->so_proto->pr_usrreqs->pr_peeraddr)(so, nam);
989 }
990
991 static int
992 gre_getnames(struct socket *so, struct lwp *l, struct sockaddr_storage *src,
993 struct sockaddr_storage *dst)
994 {
995 struct sockaddr_storage ss;
996 int rc;
997
998 solock(so);
999 if ((rc = gre_getsockname(so, (struct sockaddr *)&ss)) != 0)
1000 goto out;
1001 *src = ss;
1002
1003 if ((rc = gre_getpeername(so, (struct sockaddr *)&ss)) != 0)
1004 goto out;
1005 *dst = ss;
1006 out:
1007 sounlock(so);
1008 return rc;
1009 }
1010
1011 static void
1012 gre_fp_recvloop(void *arg)
1013 {
1014 struct gre_softc *sc = arg;
1015
1016 mutex_enter(&sc->sc_mtx);
1017 while (gre_fp_recv(sc))
1018 ;
1019 mutex_exit(&sc->sc_mtx);
1020 kthread_exit(0);
1021 }
1022
1023 static bool
1024 gre_fp_recv(struct gre_softc *sc)
1025 {
1026 int fd, ofd, rc;
1027 file_t *fp;
1028
1029 fp = sc->sc_fp;
1030 ofd = sc->sc_fd;
1031 fd = -1;
1032
1033 switch (sc->sc_msg) {
1034 case GRE_M_STOP:
1035 cv_signal(&sc->sc_fp_condvar);
1036 return false;
1037 case GRE_M_SETFP:
1038 mutex_exit(&sc->sc_mtx);
1039 rc = fd_dup(fp, 0, &fd, 0);
1040 mutex_enter(&sc->sc_mtx);
1041 if (rc != 0) {
1042 sc->sc_msg = GRE_M_ERR;
1043 break;
1044 }
1045 /*FALLTHROUGH*/
1046 case GRE_M_DELFP:
1047 mutex_exit(&sc->sc_mtx);
1048 if (ofd != -1 && fd_getfile(ofd) != NULL)
1049 fd_close(ofd);
1050 mutex_enter(&sc->sc_mtx);
1051 sc->sc_fd = fd;
1052 sc->sc_msg = GRE_M_OK;
1053 break;
1054 default:
1055 gre_fp_wait(sc);
1056 return true;
1057 }
1058 cv_signal(&sc->sc_fp_condvar);
1059 return true;
1060 }
1061
1062 static bool
1063 gre_fp_send(struct gre_softc *sc, enum gre_msg msg, file_t *fp)
1064 {
1065 bool rc;
1066
1067 mutex_enter(&sc->sc_mtx);
1068 while (sc->sc_msg != GRE_M_NONE)
1069 gre_fp_wait(sc);
1070 sc->sc_fp = fp;
1071 sc->sc_msg = msg;
1072 cv_signal(&sc->sc_fp_condvar);
1073 while (sc->sc_msg != GRE_M_STOP && sc->sc_msg != GRE_M_OK &&
1074 sc->sc_msg != GRE_M_ERR)
1075 gre_fp_wait(sc);
1076 rc = (sc->sc_msg != GRE_M_ERR);
1077 sc->sc_msg = GRE_M_NONE;
1078 cv_signal(&sc->sc_fp_condvar);
1079 mutex_exit(&sc->sc_mtx);
1080 return rc;
1081 }
1082
1083 static int
1084 gre_ssock(struct ifnet *ifp, struct gre_soparm *sp, int fd)
1085 {
1086 int error = 0;
1087 const struct protosw *pr;
1088 file_t *fp;
1089 struct gre_softc *sc = ifp->if_softc;
1090 struct socket *so;
1091 struct sockaddr_storage dst, src;
1092
1093 if ((fp = fd_getfile(fd)) == NULL)
1094 return EBADF;
1095 if (fp->f_type != DTYPE_SOCKET) {
1096 fd_putfile(fd);
1097 return ENOTSOCK;
1098 }
1099
1100 GRE_DPRINTF(sc, "\n");
1101
1102 so = fp->f_socket;
1103 pr = so->so_proto;
1104
1105 GRE_DPRINTF(sc, "type %d, proto %d\n", pr->pr_type, pr->pr_protocol);
1106
1107 if ((pr->pr_flags & PR_ATOMIC) == 0 ||
1108 (sp->sp_type != 0 && pr->pr_type != sp->sp_type) ||
1109 (sp->sp_proto != 0 && pr->pr_protocol != 0 &&
1110 pr->pr_protocol != sp->sp_proto)) {
1111 error = EINVAL;
1112 goto err;
1113 }
1114
1115 GRE_DPRINTF(sc, "\n");
1116
1117 /* check address */
1118 if ((error = gre_getnames(so, curlwp, &src, &dst)) != 0)
1119 goto err;
1120
1121 GRE_DPRINTF(sc, "\n");
1122
1123 if (!gre_fp_send(sc, GRE_M_SETFP, fp)) {
1124 error = EBUSY;
1125 goto err;
1126 }
1127
1128 GRE_DPRINTF(sc, "\n");
1129
1130 sp->sp_src = src;
1131 sp->sp_dst = dst;
1132
1133 sp->sp_so = so;
1134
1135 err:
1136 fd_putfile(fd);
1137 return error;
1138 }
1139
1140 static bool
1141 sockaddr_is_anyaddr(const struct sockaddr *sa)
1142 {
1143 socklen_t anylen, salen;
1144 const void *anyaddr, *addr;
1145
1146 if ((anyaddr = sockaddr_anyaddr(sa, &anylen)) == NULL ||
1147 (addr = sockaddr_const_addr(sa, &salen)) == NULL)
1148 return false;
1149
1150 if (salen > anylen)
1151 return false;
1152
1153 return memcmp(anyaddr, addr, MIN(anylen, salen)) == 0;
1154 }
1155
1156 static bool
1157 gre_is_nullconf(const struct gre_soparm *sp)
1158 {
1159 return sockaddr_is_anyaddr(sstocsa(&sp->sp_src)) ||
1160 sockaddr_is_anyaddr(sstocsa(&sp->sp_dst));
1161 }
1162
1163 static void
1164 gre_clearconf(struct gre_soparm *sp, bool force)
1165 {
1166 if (sp->sp_bysock || force) {
1167 sockaddr_copy(sstosa(&sp->sp_src), sizeof(sp->sp_src),
1168 sockaddr_any(sstosa(&sp->sp_src)));
1169 sockaddr_copy(sstosa(&sp->sp_dst), sizeof(sp->sp_dst),
1170 sockaddr_any(sstosa(&sp->sp_dst)));
1171 sp->sp_bysock = false;
1172 }
1173 sp->sp_so = NULL; /* XXX */
1174 }
1175
1176 static int
1177 gre_ioctl(struct ifnet *ifp, const u_long cmd, void *data)
1178 {
1179 struct ifreq *ifr;
1180 struct ifaddr *ifa = (struct ifaddr *)data;
1181 struct if_laddrreq *lifr = (struct if_laddrreq *)data;
1182 struct gre_softc *sc = ifp->if_softc;
1183 struct gre_soparm *sp;
1184 int fd, error = 0, oproto, otype, s;
1185 struct gre_soparm sp0;
1186
1187 ifr = data;
1188
1189 GRE_DPRINTF(sc, "cmd %lu\n", cmd);
1190
1191 switch (cmd) {
1192 case GRESPROTO:
1193 case GRESADDRD:
1194 case GRESADDRS:
1195 case GRESSOCK:
1196 case GREDSOCK:
1197 if (kauth_authorize_network(curlwp->l_cred,
1198 KAUTH_NETWORK_INTERFACE,
1199 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd,
1200 NULL) != 0)
1201 return EPERM;
1202 break;
1203 default:
1204 break;
1205 }
1206
1207 s = splnet();
1208
1209 sp0 = sc->sc_soparm;
1210 sp0.sp_so = NULL;
1211 sp = &sp0;
1212
1213 GRE_DPRINTF(sc, "\n");
1214
1215 switch (cmd) {
1216 case SIOCINITIFADDR:
1217 GRE_DPRINTF(sc, "\n");
1218 if ((ifp->if_flags & IFF_UP) != 0)
1219 break;
1220 gre_clearconf(sp, false);
1221 ifp->if_flags |= IFF_UP;
1222 ifa->ifa_rtrequest = p2p_rtrequest;
1223 goto mksocket;
1224 case SIOCSIFFLAGS:
1225 if ((error = ifioctl_common(ifp, cmd, data)) != 0)
1226 break;
1227 oproto = sp->sp_proto;
1228 otype = sp->sp_type;
1229 switch (ifr->ifr_flags & (IFF_LINK0|IFF_LINK2)) {
1230 case IFF_LINK0|IFF_LINK2:
1231 sp->sp_proto = IPPROTO_UDP;
1232 sp->sp_type = SOCK_DGRAM;
1233 break;
1234 case IFF_LINK2:
1235 sp->sp_proto = 0;
1236 sp->sp_type = 0;
1237 break;
1238 case IFF_LINK0:
1239 sp->sp_proto = IPPROTO_GRE;
1240 sp->sp_type = SOCK_RAW;
1241 break;
1242 default:
1243 GRE_DPRINTF(sc, "\n");
1244 error = EINVAL;
1245 goto out;
1246 }
1247 GRE_DPRINTF(sc, "\n");
1248 gre_clearconf(sp, false);
1249 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) ==
1250 (IFF_UP|IFF_RUNNING) &&
1251 (oproto == sp->sp_proto || sp->sp_proto == 0) &&
1252 (otype == sp->sp_type || sp->sp_type == 0))
1253 break;
1254 switch (sp->sp_proto) {
1255 case IPPROTO_UDP:
1256 case IPPROTO_GRE:
1257 goto mksocket;
1258 default:
1259 break;
1260 }
1261 break;
1262 case SIOCSIFMTU:
1263 /* XXX determine MTU automatically by probing w/
1264 * XXX do-not-fragment packets?
1265 */
1266 if (ifr->ifr_mtu < 576) {
1267 error = EINVAL;
1268 break;
1269 }
1270 /*FALLTHROUGH*/
1271 case SIOCGIFMTU:
1272 if ((error = ifioctl_common(ifp, cmd, data)) == ENETRESET)
1273 error = 0;
1274 break;
1275 case SIOCADDMULTI:
1276 case SIOCDELMULTI:
1277 if (ifr == NULL) {
1278 error = EAFNOSUPPORT;
1279 break;
1280 }
1281 switch (ifreq_getaddr(cmd, ifr)->sa_family) {
1282 #ifdef INET
1283 case AF_INET:
1284 break;
1285 #endif
1286 #ifdef INET6
1287 case AF_INET6:
1288 break;
1289 #endif
1290 default:
1291 error = EAFNOSUPPORT;
1292 break;
1293 }
1294 break;
1295 case GRESPROTO:
1296 gre_clearconf(sp, false);
1297 oproto = sp->sp_proto;
1298 otype = sp->sp_type;
1299 sp->sp_proto = ifr->ifr_flags;
1300 switch (sp->sp_proto) {
1301 case IPPROTO_UDP:
1302 ifp->if_flags |= IFF_LINK0|IFF_LINK2;
1303 sp->sp_type = SOCK_DGRAM;
1304 break;
1305 case IPPROTO_GRE:
1306 ifp->if_flags |= IFF_LINK0;
1307 ifp->if_flags &= ~IFF_LINK2;
1308 sp->sp_type = SOCK_RAW;
1309 break;
1310 case 0:
1311 ifp->if_flags &= ~IFF_LINK0;
1312 ifp->if_flags |= IFF_LINK2;
1313 sp->sp_type = 0;
1314 break;
1315 default:
1316 error = EPROTONOSUPPORT;
1317 break;
1318 }
1319 if ((oproto == sp->sp_proto || sp->sp_proto == 0) &&
1320 (otype == sp->sp_type || sp->sp_type == 0))
1321 break;
1322 switch (sp->sp_proto) {
1323 case IPPROTO_UDP:
1324 case IPPROTO_GRE:
1325 goto mksocket;
1326 default:
1327 break;
1328 }
1329 break;
1330 case GREGPROTO:
1331 ifr->ifr_flags = sp->sp_proto;
1332 break;
1333 case GRESADDRS:
1334 case GRESADDRD:
1335 gre_clearconf(sp, false);
1336 /* set tunnel endpoints and mark interface as up */
1337 switch (cmd) {
1338 case GRESADDRS:
1339 sockaddr_copy(sstosa(&sp->sp_src),
1340 sizeof(sp->sp_src), ifreq_getaddr(cmd, ifr));
1341 break;
1342 case GRESADDRD:
1343 sockaddr_copy(sstosa(&sp->sp_dst),
1344 sizeof(sp->sp_dst), ifreq_getaddr(cmd, ifr));
1345 break;
1346 }
1347 checkaddr:
1348 if (sockaddr_any(sstosa(&sp->sp_src)) == NULL ||
1349 sockaddr_any(sstosa(&sp->sp_dst)) == NULL) {
1350 error = EINVAL;
1351 break;
1352 }
1353 /* let gre_socreate() check the rest */
1354 mksocket:
1355 GRE_DPRINTF(sc, "\n");
1356 /* If we're administratively down, or the configuration
1357 * is empty, there's no use creating a socket.
1358 */
1359 if ((ifp->if_flags & IFF_UP) == 0 || gre_is_nullconf(sp))
1360 goto sendconf;
1361
1362 GRE_DPRINTF(sc, "\n");
1363 fd = 0;
1364 error = gre_socreate(sc, sp, &fd);
1365 if (error != 0)
1366 break;
1367
1368 setsock:
1369 GRE_DPRINTF(sc, "\n");
1370
1371 error = gre_ssock(ifp, sp, fd);
1372
1373 if (cmd != GRESSOCK) {
1374 GRE_DPRINTF(sc, "\n");
1375 /* XXX v. dodgy */
1376 if (fd_getfile(fd) != NULL)
1377 fd_close(fd);
1378 }
1379
1380 if (error == 0) {
1381 sendconf:
1382 GRE_DPRINTF(sc, "\n");
1383 ifp->if_flags &= ~IFF_RUNNING;
1384 gre_reconf(sc, sp);
1385 }
1386
1387 break;
1388 case GREGADDRS:
1389 ifreq_setaddr(cmd, ifr, sstosa(&sp->sp_src));
1390 break;
1391 case GREGADDRD:
1392 ifreq_setaddr(cmd, ifr, sstosa(&sp->sp_dst));
1393 break;
1394 case GREDSOCK:
1395 GRE_DPRINTF(sc, "\n");
1396 if (sp->sp_bysock)
1397 ifp->if_flags &= ~IFF_UP;
1398 gre_clearconf(sp, false);
1399 goto mksocket;
1400 case GRESSOCK:
1401 GRE_DPRINTF(sc, "\n");
1402 gre_clearconf(sp, true);
1403 fd = (int)ifr->ifr_value;
1404 sp->sp_bysock = true;
1405 ifp->if_flags |= IFF_UP;
1406 goto setsock;
1407 case SIOCSLIFPHYADDR:
1408 GRE_DPRINTF(sc, "\n");
1409 if (lifr->addr.ss_family != lifr->dstaddr.ss_family) {
1410 error = EAFNOSUPPORT;
1411 break;
1412 }
1413 sockaddr_copy(sstosa(&sp->sp_src), sizeof(sp->sp_src),
1414 sstosa(&lifr->addr));
1415 sockaddr_copy(sstosa(&sp->sp_dst), sizeof(sp->sp_dst),
1416 sstosa(&lifr->dstaddr));
1417 GRE_DPRINTF(sc, "\n");
1418 goto checkaddr;
1419 case SIOCDIFPHYADDR:
1420 GRE_DPRINTF(sc, "\n");
1421 gre_clearconf(sp, true);
1422 ifp->if_flags &= ~IFF_UP;
1423 goto mksocket;
1424 case SIOCGLIFPHYADDR:
1425 GRE_DPRINTF(sc, "\n");
1426 if (gre_is_nullconf(sp)) {
1427 error = EADDRNOTAVAIL;
1428 break;
1429 }
1430 sockaddr_copy(sstosa(&lifr->addr), sizeof(lifr->addr),
1431 sstosa(&sp->sp_src));
1432 sockaddr_copy(sstosa(&lifr->dstaddr), sizeof(lifr->dstaddr),
1433 sstosa(&sp->sp_dst));
1434 GRE_DPRINTF(sc, "\n");
1435 break;
1436 default:
1437 error = ifioctl_common(ifp, cmd, data);
1438 break;
1439 }
1440 out:
1441 GRE_DPRINTF(sc, "\n");
1442 splx(s);
1443 return error;
1444 }
1445
1446 /* ARGSUSED */
1447 void
1448 greattach(int count)
1449 {
1450
1451 /*
1452 * Nothing to do here, initialization is handled by the
1453 * module initialization code in greinit() below.
1454 */
1455 }
1456
1457 static void
1458 greinit(void)
1459 {
1460 if_clone_attach(&gre_cloner);
1461 }
1462
1463 static int
1464 gredetach(void)
1465 {
1466 int error = 0;
1467
1468 if (gre_count != 0)
1469 error = EBUSY;
1470
1471 if (error == 0)
1472 if_clone_detach(&gre_cloner);
1473
1474 return error;
1475 }
1476
1477 /*
1478 * Module infrastructure
1479 */
1480 #include "if_module.h"
1481
1482 IF_MODULE(MODULE_CLASS_DRIVER, gre, NULL)
1483