if_gre.c revision 1.172 1 /* $NetBSD: if_gre.c,v 1.172 2018/01/16 06:38:42 maxv 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.172 2018/01/16 06:38:42 maxv 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 <compat/sys/socket.h>
123 #include <compat/sys/sockio.h>
124
125 #include "ioconf.h"
126
127 /*
128 * It is not easy to calculate the right value for a GRE MTU.
129 * We leave this task to the admin and use the same default that
130 * other vendors use.
131 */
132 #define GREMTU 1476
133
134 #ifdef GRE_DEBUG
135 int gre_debug = 0;
136 #define GRE_DPRINTF(__sc, ...) \
137 do { \
138 if (__predict_false(gre_debug || \
139 ((__sc)->sc_if.if_flags & IFF_DEBUG) != 0)) { \
140 printf("%s.%d: ", __func__, __LINE__); \
141 printf(__VA_ARGS__); \
142 } \
143 } while (/*CONSTCOND*/0)
144 #else
145 #define GRE_DPRINTF(__sc, __fmt, ...) do { } while (/*CONSTCOND*/0)
146 #endif /* GRE_DEBUG */
147
148 int ip_gre_ttl = GRE_TTL;
149
150 static u_int gre_count;
151
152 static int gre_clone_create(struct if_clone *, int);
153 static int gre_clone_destroy(struct ifnet *);
154
155 static struct if_clone gre_cloner =
156 IF_CLONE_INITIALIZER("gre", gre_clone_create, gre_clone_destroy);
157
158 static int gre_input(struct gre_softc *, struct mbuf *, const struct gre_h *);
159 static bool gre_is_nullconf(const struct gre_soparm *);
160 static int gre_output(struct ifnet *, struct mbuf *,
161 const struct sockaddr *, const struct rtentry *);
162 static int gre_ioctl(struct ifnet *, u_long, void *);
163 static int gre_getsockname(struct socket *, struct sockaddr *);
164 static int gre_getpeername(struct socket *, struct sockaddr *);
165 static int gre_getnames(struct socket *, struct lwp *,
166 struct sockaddr_storage *, struct sockaddr_storage *);
167 static void gre_clearconf(struct gre_soparm *, bool);
168 static int gre_soreceive(struct socket *, struct mbuf **);
169 static int gre_sosend(struct socket *, struct mbuf *);
170 static struct socket *gre_reconf(struct gre_softc *, const struct gre_soparm *);
171
172 static bool gre_fp_send(struct gre_softc *, enum gre_msg, file_t *);
173 static bool gre_fp_recv(struct gre_softc *);
174 static void gre_fp_recvloop(void *);
175
176 static void
177 gre_bufq_init(struct gre_bufq *bq, size_t len0)
178 {
179 memset(bq, 0, sizeof(*bq));
180 bq->bq_q = pcq_create(len0, KM_SLEEP);
181 KASSERT(bq->bq_q != NULL);
182 }
183
184 static struct mbuf *
185 gre_bufq_dequeue(struct gre_bufq *bq)
186 {
187 return pcq_get(bq->bq_q);
188 }
189
190 static void
191 gre_bufq_purge(struct gre_bufq *bq)
192 {
193 struct mbuf *m;
194
195 while ((m = gre_bufq_dequeue(bq)) != NULL)
196 m_freem(m);
197 }
198
199 static void
200 gre_bufq_destroy(struct gre_bufq *bq)
201 {
202 gre_bufq_purge(bq);
203 pcq_destroy(bq->bq_q);
204 }
205
206 static int
207 gre_bufq_enqueue(struct gre_bufq *bq, struct mbuf *m)
208 {
209 KASSERT(bq->bq_q != NULL);
210
211 if (!pcq_put(bq->bq_q, m)) {
212 bq->bq_drops++;
213 return ENOBUFS;
214 }
215 return 0;
216 }
217
218 static void
219 greintr(void *arg)
220 {
221 struct gre_softc *sc = (struct gre_softc *)arg;
222 struct socket *so = sc->sc_soparm.sp_so;
223 int rc;
224 struct mbuf *m;
225
226 KASSERT(so != NULL);
227
228 sc->sc_send_ev.ev_count++;
229 GRE_DPRINTF(sc, "enter\n");
230 while ((m = gre_bufq_dequeue(&sc->sc_snd)) != NULL) {
231 /* XXX handle ENOBUFS? */
232 if ((rc = gre_sosend(so, m)) != 0)
233 GRE_DPRINTF(sc, "gre_sosend failed %d\n", rc);
234 }
235 }
236
237 /* Caller must hold sc->sc_mtx. */
238 static void
239 gre_fp_wait(struct gre_softc *sc)
240 {
241 sc->sc_fp_waiters++;
242 cv_wait(&sc->sc_fp_condvar, &sc->sc_mtx);
243 sc->sc_fp_waiters--;
244 }
245
246 static void
247 gre_evcnt_detach(struct gre_softc *sc)
248 {
249 evcnt_detach(&sc->sc_recv_ev);
250 evcnt_detach(&sc->sc_block_ev);
251 evcnt_detach(&sc->sc_error_ev);
252 evcnt_detach(&sc->sc_pullup_ev);
253 evcnt_detach(&sc->sc_unsupp_ev);
254
255 evcnt_detach(&sc->sc_send_ev);
256 evcnt_detach(&sc->sc_oflow_ev);
257 }
258
259 static void
260 gre_evcnt_attach(struct gre_softc *sc)
261 {
262 evcnt_attach_dynamic(&sc->sc_recv_ev, EVCNT_TYPE_MISC,
263 NULL, sc->sc_if.if_xname, "recv");
264 evcnt_attach_dynamic(&sc->sc_block_ev, EVCNT_TYPE_MISC,
265 &sc->sc_recv_ev, sc->sc_if.if_xname, "would block");
266 evcnt_attach_dynamic(&sc->sc_error_ev, EVCNT_TYPE_MISC,
267 &sc->sc_recv_ev, sc->sc_if.if_xname, "error");
268 evcnt_attach_dynamic(&sc->sc_pullup_ev, EVCNT_TYPE_MISC,
269 &sc->sc_recv_ev, sc->sc_if.if_xname, "pullup failed");
270 evcnt_attach_dynamic(&sc->sc_unsupp_ev, EVCNT_TYPE_MISC,
271 &sc->sc_recv_ev, sc->sc_if.if_xname, "unsupported");
272
273 evcnt_attach_dynamic(&sc->sc_send_ev, EVCNT_TYPE_MISC,
274 NULL, sc->sc_if.if_xname, "send");
275 evcnt_attach_dynamic(&sc->sc_oflow_ev, EVCNT_TYPE_MISC,
276 &sc->sc_send_ev, sc->sc_if.if_xname, "overflow");
277 }
278
279 static int
280 gre_clone_create(struct if_clone *ifc, int unit)
281 {
282 int rc;
283 struct gre_softc *sc;
284 struct gre_soparm *sp;
285 const struct sockaddr *any;
286
287 if ((any = sockaddr_any_by_family(AF_INET)) == NULL &&
288 (any = sockaddr_any_by_family(AF_INET6)) == NULL)
289 goto fail0;
290
291 sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK|M_ZERO);
292 mutex_init(&sc->sc_mtx, MUTEX_DRIVER, IPL_SOFTNET);
293 cv_init(&sc->sc_condvar, "gre wait");
294 cv_init(&sc->sc_fp_condvar, "gre fp");
295
296 if_initname(&sc->sc_if, ifc->ifc_name, unit);
297 sc->sc_if.if_softc = sc;
298 sc->sc_if.if_type = IFT_TUNNEL;
299 sc->sc_if.if_addrlen = 0;
300 sc->sc_if.if_hdrlen = sizeof(struct ip) + sizeof(struct gre_h);
301 sc->sc_if.if_dlt = DLT_NULL;
302 sc->sc_if.if_mtu = GREMTU;
303 sc->sc_if.if_flags = IFF_POINTOPOINT|IFF_MULTICAST;
304 sc->sc_if.if_output = gre_output;
305 sc->sc_if.if_ioctl = gre_ioctl;
306 sp = &sc->sc_soparm;
307 sockaddr_copy(sstosa(&sp->sp_dst), sizeof(sp->sp_dst), any);
308 sockaddr_copy(sstosa(&sp->sp_src), sizeof(sp->sp_src), any);
309 sp->sp_proto = IPPROTO_GRE;
310 sp->sp_type = SOCK_RAW;
311
312 sc->sc_fd = -1;
313
314 rc = kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL, gre_fp_recvloop, sc,
315 NULL, "%s", sc->sc_if.if_xname);
316 if (rc)
317 goto fail1;
318
319 gre_evcnt_attach(sc);
320
321 gre_bufq_init(&sc->sc_snd, 17);
322 sc->sc_if.if_flags |= IFF_LINK0;
323 if_attach(&sc->sc_if);
324 if_alloc_sadl(&sc->sc_if);
325 bpf_attach(&sc->sc_if, DLT_NULL, sizeof(uint32_t));
326 atomic_inc_uint(&gre_count);
327 return 0;
328
329 fail1:
330 cv_destroy(&sc->sc_fp_condvar);
331 cv_destroy(&sc->sc_condvar);
332 mutex_destroy(&sc->sc_mtx);
333 free(sc, M_DEVBUF);
334
335 fail0:
336 return -1;
337 }
338
339 static int
340 gre_clone_destroy(struct ifnet *ifp)
341 {
342 int s;
343 struct gre_softc *sc = ifp->if_softc;
344
345 GRE_DPRINTF(sc, "\n");
346
347 bpf_detach(ifp);
348 s = splnet();
349 if_detach(ifp);
350
351 GRE_DPRINTF(sc, "\n");
352 /* Note that we must not hold the mutex while we call gre_reconf(). */
353 gre_reconf(sc, NULL);
354
355 mutex_enter(&sc->sc_mtx);
356 sc->sc_msg = GRE_M_STOP;
357 cv_signal(&sc->sc_fp_condvar);
358 while (sc->sc_fp_waiters > 0)
359 cv_wait(&sc->sc_fp_condvar, &sc->sc_mtx);
360 mutex_exit(&sc->sc_mtx);
361
362 splx(s);
363
364 cv_destroy(&sc->sc_condvar);
365 cv_destroy(&sc->sc_fp_condvar);
366 mutex_destroy(&sc->sc_mtx);
367 gre_bufq_destroy(&sc->sc_snd);
368 gre_evcnt_detach(sc);
369 free(sc, M_DEVBUF);
370
371 atomic_dec_uint(&gre_count);
372 return 0;
373 }
374
375 static void
376 gre_receive(struct socket *so, void *arg, int events, int waitflag)
377 {
378 struct gre_softc *sc = (struct gre_softc *)arg;
379 int rc;
380 const struct gre_h *gh;
381 struct mbuf *m;
382
383 GRE_DPRINTF(sc, "enter\n");
384
385 sc->sc_recv_ev.ev_count++;
386
387 rc = gre_soreceive(so, &m);
388 /* TBD Back off if ECONNREFUSED (indicates
389 * ICMP Port Unreachable)?
390 */
391 if (rc == EWOULDBLOCK) {
392 GRE_DPRINTF(sc, "EWOULDBLOCK\n");
393 sc->sc_block_ev.ev_count++;
394 return;
395 } else if (rc != 0 || m == NULL) {
396 GRE_DPRINTF(sc, "%s: rc %d m %p\n",
397 sc->sc_if.if_xname, rc, (void *)m);
398 sc->sc_error_ev.ev_count++;
399 return;
400 }
401 if (m->m_len < sizeof(*gh) && (m = m_pullup(m, sizeof(*gh))) == NULL) {
402 GRE_DPRINTF(sc, "m_pullup failed\n");
403 sc->sc_pullup_ev.ev_count++;
404 return;
405 }
406 gh = mtod(m, const struct gre_h *);
407
408 if (gre_input(sc, m, gh) == 0) {
409 sc->sc_unsupp_ev.ev_count++;
410 GRE_DPRINTF(sc, "dropping unsupported\n");
411 m_freem(m);
412 }
413 }
414
415 static void
416 gre_upcall_add(struct socket *so, void *arg)
417 {
418 /* XXX What if the kernel already set an upcall? */
419 KASSERT((so->so_rcv.sb_flags & SB_UPCALL) == 0);
420 so->so_upcallarg = arg;
421 so->so_upcall = gre_receive;
422 so->so_rcv.sb_flags |= SB_UPCALL;
423 }
424
425 static void
426 gre_upcall_remove(struct socket *so)
427 {
428 so->so_rcv.sb_flags &= ~SB_UPCALL;
429 so->so_upcallarg = NULL;
430 so->so_upcall = NULL;
431 }
432
433 static int
434 gre_socreate(struct gre_softc *sc, const struct gre_soparm *sp, int *fdout)
435 {
436 int fd, rc;
437 struct socket *so;
438 struct sockaddr_big sbig;
439 sa_family_t af;
440 int val;
441
442 GRE_DPRINTF(sc, "enter\n");
443
444 af = sp->sp_src.ss_family;
445 rc = fsocreate(af, NULL, sp->sp_type, sp->sp_proto, &fd);
446 if (rc != 0) {
447 GRE_DPRINTF(sc, "fsocreate failed\n");
448 return rc;
449 }
450
451 if ((rc = fd_getsock(fd, &so)) != 0)
452 return rc;
453
454 memcpy(&sbig, &sp->sp_src, sizeof(sp->sp_src));
455 if ((rc = sobind(so, (struct sockaddr *)&sbig, curlwp)) != 0) {
456 GRE_DPRINTF(sc, "sobind failed\n");
457 goto out;
458 }
459
460 memcpy(&sbig, &sp->sp_dst, sizeof(sp->sp_dst));
461 solock(so);
462 if ((rc = soconnect(so, (struct sockaddr *)&sbig, curlwp)) != 0) {
463 GRE_DPRINTF(sc, "soconnect failed\n");
464 sounlock(so);
465 goto out;
466 }
467 sounlock(so);
468
469 /* XXX convert to a (new) SOL_SOCKET call */
470 KASSERT(so->so_proto != NULL);
471 rc = so_setsockopt(curlwp, so, IPPROTO_IP, IP_TTL,
472 &ip_gre_ttl, sizeof(ip_gre_ttl));
473 if (rc != 0) {
474 GRE_DPRINTF(sc, "so_setsockopt ttl failed\n");
475 rc = 0;
476 }
477
478 val = 1;
479 rc = so_setsockopt(curlwp, so, SOL_SOCKET, SO_NOHEADER,
480 &val, sizeof(val));
481 if (rc != 0) {
482 GRE_DPRINTF(sc, "so_setsockopt SO_NOHEADER failed\n");
483 rc = 0;
484 }
485 out:
486 if (rc != 0)
487 fd_close(fd);
488 else {
489 fd_putfile(fd);
490 *fdout = fd;
491 }
492
493 return rc;
494 }
495
496 static int
497 gre_sosend(struct socket *so, struct mbuf *top)
498 {
499 struct proc *p;
500 long space, resid;
501 int error;
502 struct lwp * const l = curlwp;
503
504 p = l->l_proc;
505
506 resid = top->m_pkthdr.len;
507 if (p)
508 l->l_ru.ru_msgsnd++;
509 #define snderr(errno) { error = errno; goto release; }
510
511 solock(so);
512 if ((error = sblock(&so->so_snd, M_NOWAIT)) != 0)
513 goto out;
514 if (so->so_state & SS_CANTSENDMORE)
515 snderr(EPIPE);
516 if (so->so_error) {
517 error = so->so_error;
518 so->so_error = 0;
519 goto release;
520 }
521 if ((so->so_state & SS_ISCONNECTED) == 0) {
522 if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
523 snderr(ENOTCONN);
524 } else {
525 snderr(EDESTADDRREQ);
526 }
527 }
528 space = sbspace(&so->so_snd);
529 if (resid > so->so_snd.sb_hiwat)
530 snderr(EMSGSIZE);
531 if (space < resid)
532 snderr(EWOULDBLOCK);
533 /*
534 * Data is prepackaged in "top".
535 */
536 if (so->so_state & SS_CANTSENDMORE)
537 snderr(EPIPE);
538 error = (*so->so_proto->pr_usrreqs->pr_send)(so,
539 top, NULL, NULL, l);
540 top = NULL;
541 release:
542 sbunlock(&so->so_snd);
543 out:
544 sounlock(so);
545 if (top != NULL)
546 m_freem(top);
547 return error;
548 }
549
550 /* This is a stripped-down version of soreceive() that will never
551 * block. It will support SOCK_DGRAM sockets. It may also support
552 * SOCK_SEQPACKET sockets.
553 */
554 static int
555 gre_soreceive(struct socket *so, struct mbuf **mp0)
556 {
557 struct mbuf *m, **mp;
558 int flags, len, error, type;
559 const struct protosw *pr;
560 struct mbuf *nextrecord;
561
562 KASSERT(mp0 != NULL);
563
564 flags = MSG_DONTWAIT;
565 pr = so->so_proto;
566 mp = mp0;
567 type = 0;
568
569 *mp = NULL;
570
571 KASSERT(pr->pr_flags & PR_ATOMIC);
572 restart:
573 if ((error = sblock(&so->so_rcv, M_NOWAIT)) != 0) {
574 return error;
575 }
576 m = so->so_rcv.sb_mb;
577 /*
578 * If we have less data than requested, do not block awaiting more.
579 */
580 if (m == NULL) {
581 #ifdef DIAGNOSTIC
582 if (so->so_rcv.sb_cc)
583 panic("receive 1");
584 #endif
585 if (so->so_error) {
586 error = so->so_error;
587 so->so_error = 0;
588 } else if (so->so_state & SS_CANTRCVMORE)
589 ;
590 else if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0
591 && (so->so_proto->pr_flags & PR_CONNREQUIRED))
592 error = ENOTCONN;
593 else
594 error = EWOULDBLOCK;
595 goto release;
596 }
597 /*
598 * On entry here, m points to the first record of the socket buffer.
599 * While we process the initial mbufs containing address and control
600 * info, we save a copy of m->m_nextpkt into nextrecord.
601 */
602 if (curlwp != NULL)
603 curlwp->l_ru.ru_msgrcv++;
604 KASSERT(m == so->so_rcv.sb_mb);
605 SBLASTRECORDCHK(&so->so_rcv, "soreceive 1");
606 SBLASTMBUFCHK(&so->so_rcv, "soreceive 1");
607 nextrecord = m->m_nextpkt;
608 if (pr->pr_flags & PR_ADDR) {
609 #ifdef DIAGNOSTIC
610 if (m->m_type != MT_SONAME)
611 panic("receive 1a");
612 #endif
613 sbfree(&so->so_rcv, m);
614 m = so->so_rcv.sb_mb = m_free(m);
615 }
616 while (m != NULL && m->m_type == MT_CONTROL && error == 0) {
617 sbfree(&so->so_rcv, m);
618 /*
619 * Dispose of any SCM_RIGHTS message that went
620 * through the read path rather than recv.
621 */
622 if (pr->pr_domain->dom_dispose &&
623 mtod(m, struct cmsghdr *)->cmsg_type == SCM_RIGHTS)
624 (*pr->pr_domain->dom_dispose)(m);
625 m = so->so_rcv.sb_mb = m_free(m);
626 }
627
628 /*
629 * If m is non-NULL, we have some data to read. From now on,
630 * make sure to keep sb_lastrecord consistent when working on
631 * the last packet on the chain (nextrecord == NULL) and we
632 * change m->m_nextpkt.
633 */
634 if (m != NULL) {
635 m->m_nextpkt = nextrecord;
636 /*
637 * If nextrecord == NULL (this is a single chain),
638 * then sb_lastrecord may not be valid here if m
639 * was changed earlier.
640 */
641 if (nextrecord == NULL) {
642 KASSERT(so->so_rcv.sb_mb == m);
643 so->so_rcv.sb_lastrecord = m;
644 }
645 type = m->m_type;
646 if (type == MT_OOBDATA)
647 flags |= MSG_OOB;
648 } else {
649 KASSERT(so->so_rcv.sb_mb == m);
650 so->so_rcv.sb_mb = nextrecord;
651 SB_EMPTY_FIXUP(&so->so_rcv);
652 }
653 SBLASTRECORDCHK(&so->so_rcv, "soreceive 2");
654 SBLASTMBUFCHK(&so->so_rcv, "soreceive 2");
655
656 while (m != NULL) {
657 if (m->m_type == MT_OOBDATA) {
658 if (type != MT_OOBDATA)
659 break;
660 } else if (type == MT_OOBDATA)
661 break;
662 #ifdef DIAGNOSTIC
663 else if (m->m_type != MT_DATA && m->m_type != MT_HEADER)
664 panic("receive 3");
665 #endif
666 so->so_state &= ~SS_RCVATMARK;
667 if (so->so_oobmark != 0 && so->so_oobmark < m->m_len)
668 break;
669 len = m->m_len;
670 /*
671 * mp is set, just pass back the mbufs.
672 * Sockbuf must be consistent here (points to current mbuf,
673 * it points to next record) when we drop priority;
674 * we must note any additions to the sockbuf when we
675 * block interrupts again.
676 */
677 if (m->m_flags & M_EOR)
678 flags |= MSG_EOR;
679 nextrecord = m->m_nextpkt;
680 sbfree(&so->so_rcv, m);
681 *mp = m;
682 mp = &m->m_next;
683 so->so_rcv.sb_mb = m = m->m_next;
684 *mp = NULL;
685 /*
686 * If m != NULL, we also know that
687 * so->so_rcv.sb_mb != NULL.
688 */
689 KASSERT(so->so_rcv.sb_mb == m);
690 if (m) {
691 m->m_nextpkt = nextrecord;
692 if (nextrecord == NULL)
693 so->so_rcv.sb_lastrecord = m;
694 } else {
695 so->so_rcv.sb_mb = nextrecord;
696 SB_EMPTY_FIXUP(&so->so_rcv);
697 }
698 SBLASTRECORDCHK(&so->so_rcv, "soreceive 3");
699 SBLASTMBUFCHK(&so->so_rcv, "soreceive 3");
700 if (so->so_oobmark) {
701 so->so_oobmark -= len;
702 if (so->so_oobmark == 0) {
703 so->so_state |= SS_RCVATMARK;
704 break;
705 }
706 }
707 if (flags & MSG_EOR)
708 break;
709 }
710
711 if (m != NULL) {
712 m_freem(*mp);
713 *mp = NULL;
714 error = ENOMEM;
715 (void) sbdroprecord(&so->so_rcv);
716 } else {
717 /*
718 * First part is an inline SB_EMPTY_FIXUP(). Second
719 * part makes sure sb_lastrecord is up-to-date if
720 * there is still data in the socket buffer.
721 */
722 so->so_rcv.sb_mb = nextrecord;
723 if (so->so_rcv.sb_mb == NULL) {
724 so->so_rcv.sb_mbtail = NULL;
725 so->so_rcv.sb_lastrecord = NULL;
726 } else if (nextrecord->m_nextpkt == NULL)
727 so->so_rcv.sb_lastrecord = nextrecord;
728 }
729 SBLASTRECORDCHK(&so->so_rcv, "soreceive 4");
730 SBLASTMBUFCHK(&so->so_rcv, "soreceive 4");
731 if (pr->pr_flags & PR_WANTRCVD && so->so_pcb)
732 (*pr->pr_usrreqs->pr_rcvd)(so, flags, curlwp);
733 if (*mp0 == NULL && (flags & MSG_EOR) == 0 &&
734 (so->so_state & SS_CANTRCVMORE) == 0) {
735 sbunlock(&so->so_rcv);
736 goto restart;
737 }
738
739 release:
740 sbunlock(&so->so_rcv);
741 return error;
742 }
743
744 static struct socket *
745 gre_reconf(struct gre_softc *sc, const struct gre_soparm *newsoparm)
746 {
747 struct ifnet *ifp = &sc->sc_if;
748
749 GRE_DPRINTF(sc, "enter\n");
750
751 shutdown:
752 if (sc->sc_soparm.sp_so != NULL) {
753 GRE_DPRINTF(sc, "\n");
754 gre_upcall_remove(sc->sc_soparm.sp_so);
755 softint_disestablish(sc->sc_si);
756 sc->sc_si = NULL;
757 gre_fp_send(sc, GRE_M_DELFP, NULL);
758 gre_clearconf(&sc->sc_soparm, false);
759 }
760
761 if (newsoparm != NULL) {
762 GRE_DPRINTF(sc, "\n");
763 sc->sc_soparm = *newsoparm;
764 newsoparm = NULL;
765 }
766
767 if (sc->sc_soparm.sp_so != NULL) {
768 GRE_DPRINTF(sc, "\n");
769 sc->sc_si = softint_establish(SOFTINT_NET, greintr, sc);
770 gre_upcall_add(sc->sc_soparm.sp_so, sc);
771 if ((ifp->if_flags & IFF_UP) == 0) {
772 GRE_DPRINTF(sc, "down\n");
773 goto shutdown;
774 }
775 }
776
777 GRE_DPRINTF(sc, "\n");
778 if (sc->sc_soparm.sp_so != NULL)
779 sc->sc_if.if_flags |= IFF_RUNNING;
780 else {
781 gre_bufq_purge(&sc->sc_snd);
782 sc->sc_if.if_flags &= ~IFF_RUNNING;
783 }
784 return sc->sc_soparm.sp_so;
785 }
786
787 static int
788 gre_input(struct gre_softc *sc, struct mbuf *m, const struct gre_h *gh)
789 {
790 pktqueue_t *pktq = NULL;
791 struct ifqueue *ifq = NULL;
792 uint16_t flags;
793 uint32_t af; /* af passed to BPF tap */
794 int isr = 0, s;
795 int hlen;
796
797 sc->sc_if.if_ipackets++;
798 sc->sc_if.if_ibytes += m->m_pkthdr.len;
799
800 hlen = sizeof(struct gre_h);
801
802 /* process GRE flags as packet can be of variable len */
803 flags = ntohs(gh->flags);
804
805 /* Checksum & Offset are present */
806 if ((flags & GRE_CP) | (flags & GRE_RP))
807 hlen += 4;
808 /* We don't support routing fields (variable length) */
809 if (flags & GRE_RP) {
810 sc->sc_if.if_ierrors++;
811 return 0;
812 }
813 if (flags & GRE_KP)
814 hlen += 4;
815 if (flags & GRE_SP)
816 hlen += 4;
817
818 switch (ntohs(gh->ptype)) { /* ethertypes */
819 #ifdef INET
820 case ETHERTYPE_IP:
821 pktq = ip_pktq;
822 af = AF_INET;
823 break;
824 #endif
825 #ifdef NETATALK
826 case ETHERTYPE_ATALK:
827 ifq = &atintrq1;
828 isr = NETISR_ATALK;
829 af = AF_APPLETALK;
830 break;
831 #endif
832 #ifdef INET6
833 case ETHERTYPE_IPV6:
834 pktq = ip6_pktq;
835 af = AF_INET6;
836 break;
837 #endif
838 #ifdef MPLS
839 case ETHERTYPE_MPLS:
840 ifq = &mplsintrq;
841 isr = NETISR_MPLS;
842 af = AF_MPLS;
843 break;
844 #endif
845 default: /* others not yet supported */
846 GRE_DPRINTF(sc, "unhandled ethertype 0x%04x\n",
847 ntohs(gh->ptype));
848 sc->sc_if.if_noproto++;
849 return 0;
850 }
851
852 if (hlen > m->m_pkthdr.len) {
853 m_freem(m);
854 sc->sc_if.if_ierrors++;
855 return 1;
856 }
857 m_adj(m, hlen);
858
859 bpf_mtap_af(&sc->sc_if, af, m);
860
861 m_set_rcvif(m, &sc->sc_if);
862
863 if (__predict_true(pktq)) {
864 if (__predict_false(!pktq_enqueue(pktq, m, 0))) {
865 m_freem(m);
866 }
867 return 1;
868 }
869
870 s = splnet();
871 if (IF_QFULL(ifq)) {
872 IF_DROP(ifq);
873 m_freem(m);
874 } else {
875 IF_ENQUEUE(ifq, m);
876 }
877 /* we need schednetisr since the address family may change */
878 schednetisr(isr);
879 splx(s);
880
881 return 1; /* packet is done, no further processing needed */
882 }
883
884 /*
885 * The output routine. Takes a packet and encapsulates it in the protocol
886 * given by sc->sc_soparm.sp_proto. See also RFC 1701 and RFC 2004
887 */
888 static int
889 gre_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
890 const struct rtentry *rt)
891 {
892 int error = 0;
893 struct gre_softc *sc = ifp->if_softc;
894 struct gre_h *gh;
895 uint16_t etype = 0;
896
897 KASSERT((m->m_flags & M_PKTHDR) != 0);
898
899 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) {
900 m_freem(m);
901 error = ENETDOWN;
902 goto end;
903 }
904
905 bpf_mtap_af(ifp, dst->sa_family, m);
906
907 m->m_flags &= ~(M_BCAST|M_MCAST);
908
909 GRE_DPRINTF(sc, "dst->sa_family=%d\n", dst->sa_family);
910 switch (dst->sa_family) {
911 #ifdef INET
912 case AF_INET:
913 /*
914 * TBD Extract the IP ToS field and set the
915 * encapsulating protocol's ToS to suit.
916 */
917 etype = htons(ETHERTYPE_IP);
918 break;
919 #endif
920 #ifdef NETATALK
921 case AF_APPLETALK:
922 etype = htons(ETHERTYPE_ATALK);
923 break;
924 #endif
925 #ifdef INET6
926 case AF_INET6:
927 etype = htons(ETHERTYPE_IPV6);
928 break;
929 #endif
930 default:
931 IF_DROP(&ifp->if_snd);
932 m_freem(m);
933 error = EAFNOSUPPORT;
934 goto end;
935 }
936
937 #ifdef MPLS
938 if (rt != NULL && rt_gettag(rt) != NULL) {
939 union mpls_shim msh;
940 msh.s_addr = MPLS_GETSADDR(rt);
941 if (msh.shim.label != MPLS_LABEL_IMPLNULL)
942 etype = htons(ETHERTYPE_MPLS);
943 }
944 #endif
945
946 M_PREPEND(m, sizeof(*gh), M_DONTWAIT);
947
948 if (m == NULL) {
949 IF_DROP(&ifp->if_snd);
950 error = ENOBUFS;
951 goto end;
952 }
953
954 gh = mtod(m, struct gre_h *);
955 gh->flags = 0;
956 gh->ptype = etype;
957 /* XXX Need to handle IP ToS. Look at how I handle IP TTL. */
958
959 ifp->if_opackets++;
960 ifp->if_obytes += m->m_pkthdr.len;
961
962 /* Clear checksum-offload flags. */
963 m->m_pkthdr.csum_flags = 0;
964 m->m_pkthdr.csum_data = 0;
965
966 /* send it off */
967 if ((error = gre_bufq_enqueue(&sc->sc_snd, m)) != 0) {
968 sc->sc_oflow_ev.ev_count++;
969 m_freem(m);
970 } else
971 softint_schedule(sc->sc_si);
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, "")
1483