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