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