if_gre.c revision 1.150.14.2 1 /* $NetBSD: if_gre.c,v 1.150.14.2 2014/05/18 17:46:12 rmind 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.150.14.2 2014/05/18 17:46:12 rmind 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 mbuf *, struct lwp *);
153 static int gre_getpeername(struct socket *, struct mbuf *, struct lwp *);
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 return -1;
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
306 if (rc != 0)
307 return -1;
308
309 gre_evcnt_attach(sc);
310
311 gre_bufq_init(&sc->sc_snd, 17);
312 sc->sc_if.if_flags |= IFF_LINK0;
313 if_attach(&sc->sc_if);
314 if_alloc_sadl(&sc->sc_if);
315 bpf_attach(&sc->sc_if, DLT_NULL, sizeof(uint32_t));
316 return 0;
317 }
318
319 static int
320 gre_clone_destroy(struct ifnet *ifp)
321 {
322 int s;
323 struct gre_softc *sc = ifp->if_softc;
324
325 GRE_DPRINTF(sc, "\n");
326
327 bpf_detach(ifp);
328 s = splnet();
329 if_detach(ifp);
330
331 GRE_DPRINTF(sc, "\n");
332 /* Note that we must not hold the mutex while we call gre_reconf(). */
333 gre_reconf(sc, NULL);
334
335 mutex_enter(&sc->sc_mtx);
336 sc->sc_msg = GRE_M_STOP;
337 cv_signal(&sc->sc_fp_condvar);
338 while (sc->sc_fp_waiters > 0)
339 cv_wait(&sc->sc_fp_condvar, &sc->sc_mtx);
340 mutex_exit(&sc->sc_mtx);
341
342 splx(s);
343
344 cv_destroy(&sc->sc_condvar);
345 cv_destroy(&sc->sc_fp_condvar);
346 mutex_destroy(&sc->sc_mtx);
347 gre_bufq_destroy(&sc->sc_snd);
348 gre_evcnt_detach(sc);
349 free(sc, M_DEVBUF);
350
351 return 0;
352 }
353
354 static void
355 gre_receive(struct socket *so, void *arg, int events, int waitflag)
356 {
357 struct gre_softc *sc = (struct gre_softc *)arg;
358 int rc;
359 const struct gre_h *gh;
360 struct mbuf *m;
361
362 GRE_DPRINTF(sc, "enter\n");
363
364 sc->sc_recv_ev.ev_count++;
365
366 rc = gre_soreceive(so, &m);
367 /* TBD Back off if ECONNREFUSED (indicates
368 * ICMP Port Unreachable)?
369 */
370 if (rc == EWOULDBLOCK) {
371 GRE_DPRINTF(sc, "EWOULDBLOCK\n");
372 sc->sc_block_ev.ev_count++;
373 return;
374 } else if (rc != 0 || m == NULL) {
375 GRE_DPRINTF(sc, "%s: rc %d m %p\n",
376 sc->sc_if.if_xname, rc, (void *)m);
377 sc->sc_error_ev.ev_count++;
378 return;
379 }
380 if (m->m_len < sizeof(*gh) && (m = m_pullup(m, sizeof(*gh))) == NULL) {
381 GRE_DPRINTF(sc, "m_pullup failed\n");
382 sc->sc_pullup_ev.ev_count++;
383 return;
384 }
385 gh = mtod(m, const struct gre_h *);
386
387 if (gre_input(sc, m, 0, gh) == 0) {
388 sc->sc_unsupp_ev.ev_count++;
389 GRE_DPRINTF(sc, "dropping unsupported\n");
390 m_freem(m);
391 }
392 }
393
394 static void
395 gre_upcall_add(struct socket *so, void *arg)
396 {
397 /* XXX What if the kernel already set an upcall? */
398 KASSERT((so->so_rcv.sb_flags & SB_UPCALL) == 0);
399 so->so_upcallarg = arg;
400 so->so_upcall = gre_receive;
401 so->so_rcv.sb_flags |= SB_UPCALL;
402 }
403
404 static void
405 gre_upcall_remove(struct socket *so)
406 {
407 so->so_rcv.sb_flags &= ~SB_UPCALL;
408 so->so_upcallarg = NULL;
409 so->so_upcall = NULL;
410 }
411
412 static int
413 gre_socreate(struct gre_softc *sc, const struct gre_soparm *sp, int *fdout)
414 {
415 int fd, rc;
416 struct mbuf *m;
417 struct sockaddr *sa;
418 struct socket *so;
419 sa_family_t af;
420 int val;
421
422 GRE_DPRINTF(sc, "enter\n");
423
424 af = sp->sp_src.ss_family;
425 rc = fsocreate(af, NULL, sp->sp_type, sp->sp_proto, &fd);
426 if (rc != 0) {
427 GRE_DPRINTF(sc, "fsocreate failed\n");
428 return rc;
429 }
430
431 if ((rc = fd_getsock(fd, &so)) != 0)
432 return rc;
433
434 if ((m = getsombuf(so, MT_SONAME)) == NULL) {
435 rc = ENOBUFS;
436 goto out;
437 }
438 sa = mtod(m, struct sockaddr *);
439 sockaddr_copy(sa, MIN(MLEN, sizeof(sp->sp_src)), sstocsa(&sp->sp_src));
440 m->m_len = sp->sp_src.ss_len;
441
442 if ((rc = sobind(so, m, curlwp)) != 0) {
443 GRE_DPRINTF(sc, "sobind failed\n");
444 goto out;
445 }
446
447 sockaddr_copy(sa, MIN(MLEN, sizeof(sp->sp_dst)), sstocsa(&sp->sp_dst));
448 m->m_len = sp->sp_dst.ss_len;
449
450 solock(so);
451 if ((rc = soconnect(so, m, curlwp)) != 0) {
452 GRE_DPRINTF(sc, "soconnect failed\n");
453 sounlock(so);
454 goto out;
455 }
456 sounlock(so);
457
458 m = NULL;
459
460 /* XXX convert to a (new) SOL_SOCKET call */
461 KASSERT(so->so_proto != NULL);
462 rc = so_setsockopt(curlwp, so, IPPROTO_IP, IP_TTL,
463 &ip_gre_ttl, sizeof(ip_gre_ttl));
464 if (rc != 0) {
465 GRE_DPRINTF(sc, "so_setsockopt ttl failed\n");
466 rc = 0;
467 }
468
469 val = 1;
470 rc = so_setsockopt(curlwp, so, SOL_SOCKET, SO_NOHEADER,
471 &val, sizeof(val));
472 if (rc != 0) {
473 GRE_DPRINTF(sc, "so_setsockopt SO_NOHEADER failed\n");
474 rc = 0;
475 }
476 out:
477 m_freem(m);
478
479 if (rc != 0)
480 fd_close(fd);
481 else {
482 fd_putfile(fd);
483 *fdout = fd;
484 }
485
486 return rc;
487 }
488
489 static int
490 gre_sosend(struct socket *so, struct mbuf *top)
491 {
492 struct proc *p;
493 long space, resid;
494 int error;
495 struct lwp * const l = curlwp;
496
497 p = l->l_proc;
498
499 resid = top->m_pkthdr.len;
500 if (p)
501 l->l_ru.ru_msgsnd++;
502 #define snderr(errno) { error = errno; goto release; }
503
504 solock(so);
505 if ((error = sblock(&so->so_snd, M_NOWAIT)) != 0)
506 goto out;
507 if (so->so_state & SS_CANTSENDMORE)
508 snderr(EPIPE);
509 if (so->so_error) {
510 error = so->so_error;
511 so->so_error = 0;
512 goto release;
513 }
514 if ((so->so_state & SS_ISCONNECTED) == 0) {
515 if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
516 snderr(ENOTCONN);
517 } else {
518 snderr(EDESTADDRREQ);
519 }
520 }
521 space = sbspace(&so->so_snd);
522 if (resid > so->so_snd.sb_hiwat)
523 snderr(EMSGSIZE);
524 if (space < resid)
525 snderr(EWOULDBLOCK);
526 /*
527 * Data is prepackaged in "top".
528 */
529 if (so->so_state & SS_CANTSENDMORE)
530 snderr(EPIPE);
531 error = (*so->so_proto->pr_usrreqs->pr_generic)(so,
532 PRU_SEND, top, NULL, NULL, l);
533 top = NULL;
534 release:
535 sbunlock(&so->so_snd);
536 out:
537 sounlock(so);
538 if (top != NULL)
539 m_freem(top);
540 return error;
541 }
542
543 /* This is a stripped-down version of soreceive() that will never
544 * block. It will support SOCK_DGRAM sockets. It may also support
545 * SOCK_SEQPACKET sockets.
546 */
547 static int
548 gre_soreceive(struct socket *so, struct mbuf **mp0)
549 {
550 struct mbuf *m, **mp;
551 int flags, len, error, type;
552 const struct protosw *pr;
553 struct mbuf *nextrecord;
554
555 KASSERT(mp0 != NULL);
556
557 flags = MSG_DONTWAIT;
558 pr = so->so_proto;
559 mp = mp0;
560 type = 0;
561
562 *mp = NULL;
563
564 KASSERT(pr->pr_flags & PR_ATOMIC);
565 restart:
566 if ((error = sblock(&so->so_rcv, M_NOWAIT)) != 0) {
567 return error;
568 }
569 m = so->so_rcv.sb_mb;
570 /*
571 * If we have less data than requested, do not block awaiting more.
572 */
573 if (m == NULL) {
574 #ifdef DIAGNOSTIC
575 if (so->so_rcv.sb_cc)
576 panic("receive 1");
577 #endif
578 if (so->so_error) {
579 error = so->so_error;
580 so->so_error = 0;
581 } else if (so->so_state & SS_CANTRCVMORE)
582 ;
583 else if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0
584 && (so->so_proto->pr_flags & PR_CONNREQUIRED))
585 error = ENOTCONN;
586 else
587 error = EWOULDBLOCK;
588 goto release;
589 }
590 /*
591 * On entry here, m points to the first record of the socket buffer.
592 * While we process the initial mbufs containing address and control
593 * info, we save a copy of m->m_nextpkt into nextrecord.
594 */
595 if (curlwp != NULL)
596 curlwp->l_ru.ru_msgrcv++;
597 KASSERT(m == so->so_rcv.sb_mb);
598 SBLASTRECORDCHK(&so->so_rcv, "soreceive 1");
599 SBLASTMBUFCHK(&so->so_rcv, "soreceive 1");
600 nextrecord = m->m_nextpkt;
601 if (pr->pr_flags & PR_ADDR) {
602 #ifdef DIAGNOSTIC
603 if (m->m_type != MT_SONAME)
604 panic("receive 1a");
605 #endif
606 sbfree(&so->so_rcv, m);
607 MFREE(m, so->so_rcv.sb_mb);
608 m = so->so_rcv.sb_mb;
609 }
610 while (m != NULL && m->m_type == MT_CONTROL && error == 0) {
611 sbfree(&so->so_rcv, m);
612 /*
613 * Dispose of any SCM_RIGHTS message that went
614 * through the read path rather than recv.
615 */
616 if (pr->pr_domain->dom_dispose &&
617 mtod(m, struct cmsghdr *)->cmsg_type == SCM_RIGHTS)
618 (*pr->pr_domain->dom_dispose)(m);
619 MFREE(m, so->so_rcv.sb_mb);
620 m = so->so_rcv.sb_mb;
621 }
622
623 /*
624 * If m is non-NULL, we have some data to read. From now on,
625 * make sure to keep sb_lastrecord consistent when working on
626 * the last packet on the chain (nextrecord == NULL) and we
627 * change m->m_nextpkt.
628 */
629 if (m != NULL) {
630 m->m_nextpkt = nextrecord;
631 /*
632 * If nextrecord == NULL (this is a single chain),
633 * then sb_lastrecord may not be valid here if m
634 * was changed earlier.
635 */
636 if (nextrecord == NULL) {
637 KASSERT(so->so_rcv.sb_mb == m);
638 so->so_rcv.sb_lastrecord = m;
639 }
640 type = m->m_type;
641 if (type == MT_OOBDATA)
642 flags |= MSG_OOB;
643 } else {
644 KASSERT(so->so_rcv.sb_mb == m);
645 so->so_rcv.sb_mb = nextrecord;
646 SB_EMPTY_FIXUP(&so->so_rcv);
647 }
648 SBLASTRECORDCHK(&so->so_rcv, "soreceive 2");
649 SBLASTMBUFCHK(&so->so_rcv, "soreceive 2");
650
651 while (m != NULL) {
652 if (m->m_type == MT_OOBDATA) {
653 if (type != MT_OOBDATA)
654 break;
655 } else if (type == MT_OOBDATA)
656 break;
657 #ifdef DIAGNOSTIC
658 else if (m->m_type != MT_DATA && m->m_type != MT_HEADER)
659 panic("receive 3");
660 #endif
661 so->so_state &= ~SS_RCVATMARK;
662 if (so->so_oobmark != 0 && so->so_oobmark < m->m_len)
663 break;
664 len = m->m_len;
665 /*
666 * mp is set, just pass back the mbufs.
667 * Sockbuf must be consistent here (points to current mbuf,
668 * it points to next record) when we drop priority;
669 * we must note any additions to the sockbuf when we
670 * block interrupts again.
671 */
672 if (m->m_flags & M_EOR)
673 flags |= MSG_EOR;
674 nextrecord = m->m_nextpkt;
675 sbfree(&so->so_rcv, m);
676 *mp = m;
677 mp = &m->m_next;
678 so->so_rcv.sb_mb = m = m->m_next;
679 *mp = NULL;
680 /*
681 * If m != NULL, we also know that
682 * so->so_rcv.sb_mb != NULL.
683 */
684 KASSERT(so->so_rcv.sb_mb == m);
685 if (m) {
686 m->m_nextpkt = nextrecord;
687 if (nextrecord == NULL)
688 so->so_rcv.sb_lastrecord = m;
689 } else {
690 so->so_rcv.sb_mb = nextrecord;
691 SB_EMPTY_FIXUP(&so->so_rcv);
692 }
693 SBLASTRECORDCHK(&so->so_rcv, "soreceive 3");
694 SBLASTMBUFCHK(&so->so_rcv, "soreceive 3");
695 if (so->so_oobmark) {
696 so->so_oobmark -= len;
697 if (so->so_oobmark == 0) {
698 so->so_state |= SS_RCVATMARK;
699 break;
700 }
701 }
702 if (flags & MSG_EOR)
703 break;
704 }
705
706 if (m != NULL) {
707 m_freem(*mp);
708 *mp = NULL;
709 error = ENOMEM;
710 (void) sbdroprecord(&so->so_rcv);
711 } else {
712 /*
713 * First part is an inline SB_EMPTY_FIXUP(). Second
714 * part makes sure sb_lastrecord is up-to-date if
715 * there is still data in the socket buffer.
716 */
717 so->so_rcv.sb_mb = nextrecord;
718 if (so->so_rcv.sb_mb == NULL) {
719 so->so_rcv.sb_mbtail = NULL;
720 so->so_rcv.sb_lastrecord = NULL;
721 } else if (nextrecord->m_nextpkt == NULL)
722 so->so_rcv.sb_lastrecord = nextrecord;
723 }
724 SBLASTRECORDCHK(&so->so_rcv, "soreceive 4");
725 SBLASTMBUFCHK(&so->so_rcv, "soreceive 4");
726 if (pr->pr_flags & PR_WANTRCVD && so->so_pcb)
727 (*pr->pr_usrreqs->pr_generic)(so, PRU_RCVD, NULL,
728 (struct mbuf *)(long)flags, NULL, curlwp);
729 if (*mp0 == NULL && (flags & MSG_EOR) == 0 &&
730 (so->so_state & SS_CANTRCVMORE) == 0) {
731 sbunlock(&so->so_rcv);
732 goto restart;
733 }
734
735 release:
736 sbunlock(&so->so_rcv);
737 return error;
738 }
739
740 static struct socket *
741 gre_reconf(struct gre_softc *sc, const struct gre_soparm *newsoparm)
742 {
743 struct ifnet *ifp = &sc->sc_if;
744
745 GRE_DPRINTF(sc, "enter\n");
746
747 shutdown:
748 if (sc->sc_soparm.sp_so != NULL) {
749 GRE_DPRINTF(sc, "\n");
750 gre_upcall_remove(sc->sc_soparm.sp_so);
751 softint_disestablish(sc->sc_si);
752 sc->sc_si = NULL;
753 gre_fp_send(sc, GRE_M_DELFP, NULL);
754 gre_clearconf(&sc->sc_soparm, false);
755 }
756
757 if (newsoparm != NULL) {
758 GRE_DPRINTF(sc, "\n");
759 sc->sc_soparm = *newsoparm;
760 newsoparm = NULL;
761 }
762
763 if (sc->sc_soparm.sp_so != NULL) {
764 GRE_DPRINTF(sc, "\n");
765 sc->sc_si = softint_establish(SOFTINT_NET, greintr, sc);
766 gre_upcall_add(sc->sc_soparm.sp_so, sc);
767 if ((ifp->if_flags & IFF_UP) == 0) {
768 GRE_DPRINTF(sc, "down\n");
769 goto shutdown;
770 }
771 }
772
773 GRE_DPRINTF(sc, "\n");
774 if (sc->sc_soparm.sp_so != NULL)
775 sc->sc_if.if_flags |= IFF_RUNNING;
776 else {
777 gre_bufq_purge(&sc->sc_snd);
778 sc->sc_if.if_flags &= ~IFF_RUNNING;
779 }
780 return sc->sc_soparm.sp_so;
781 }
782
783 static int
784 gre_input(struct gre_softc *sc, struct mbuf *m, int hlen,
785 const struct gre_h *gh)
786 {
787 uint16_t flags;
788 uint32_t af; /* af passed to BPF tap */
789 int isr, s;
790 struct ifqueue *ifq;
791
792 sc->sc_if.if_ipackets++;
793 sc->sc_if.if_ibytes += m->m_pkthdr.len;
794
795 hlen += sizeof(struct gre_h);
796
797 /* process GRE flags as packet can be of variable len */
798 flags = ntohs(gh->flags);
799
800 /* Checksum & Offset are present */
801 if ((flags & GRE_CP) | (flags & GRE_RP))
802 hlen += 4;
803 /* We don't support routing fields (variable length) */
804 if (flags & GRE_RP) {
805 sc->sc_if.if_ierrors++;
806 return 0;
807 }
808 if (flags & GRE_KP)
809 hlen += 4;
810 if (flags & GRE_SP)
811 hlen += 4;
812
813 switch (ntohs(gh->ptype)) { /* ethertypes */
814 #ifdef INET
815 case ETHERTYPE_IP:
816 ifq = &ipintrq;
817 isr = NETISR_IP;
818 af = AF_INET;
819 break;
820 #endif
821 #ifdef NETATALK
822 case ETHERTYPE_ATALK:
823 ifq = &atintrq1;
824 isr = NETISR_ATALK;
825 af = AF_APPLETALK;
826 break;
827 #endif
828 #ifdef INET6
829 case ETHERTYPE_IPV6:
830 ifq = &ip6intrq;
831 isr = NETISR_IPV6;
832 af = AF_INET6;
833 break;
834 #endif
835 #ifdef MPLS
836 case ETHERTYPE_MPLS:
837 ifq = &mplsintrq;
838 isr = NETISR_MPLS;
839 af = AF_MPLS;
840 break;
841 #endif
842 default: /* others not yet supported */
843 GRE_DPRINTF(sc, "unhandled ethertype 0x%04x\n",
844 ntohs(gh->ptype));
845 sc->sc_if.if_noproto++;
846 return 0;
847 }
848
849 if (hlen > m->m_pkthdr.len) {
850 m_freem(m);
851 sc->sc_if.if_ierrors++;
852 return EINVAL;
853 }
854 m_adj(m, hlen);
855
856 bpf_mtap_af(&sc->sc_if, af, m);
857
858 m->m_pkthdr.rcvif = &sc->sc_if;
859
860 s = splnet();
861 if (IF_QFULL(ifq)) {
862 IF_DROP(ifq);
863 m_freem(m);
864 } else {
865 IF_ENQUEUE(ifq, m);
866 }
867 /* we need schednetisr since the address family may change */
868 schednetisr(isr);
869 splx(s);
870
871 return 1; /* packet is done, no further processing needed */
872 }
873
874 /*
875 * The output routine. Takes a packet and encapsulates it in the protocol
876 * given by sc->sc_soparm.sp_proto. See also RFC 1701 and RFC 2004
877 */
878 static int
879 gre_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
880 struct rtentry *rt)
881 {
882 int error = 0;
883 struct gre_softc *sc = ifp->if_softc;
884 struct gre_h *gh;
885 uint16_t etype = 0;
886
887 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) {
888 m_freem(m);
889 error = ENETDOWN;
890 goto end;
891 }
892
893 bpf_mtap_af(ifp, dst->sa_family, m);
894
895 m->m_flags &= ~(M_BCAST|M_MCAST);
896
897 GRE_DPRINTF(sc, "dst->sa_family=%d\n", dst->sa_family);
898 switch (dst->sa_family) {
899 #ifdef INET
900 case AF_INET:
901 /* TBD Extract the IP ToS field and set the
902 * encapsulating protocol's ToS to suit.
903 */
904 etype = htons(ETHERTYPE_IP);
905 break;
906 #endif
907 #ifdef NETATALK
908 case AF_APPLETALK:
909 etype = htons(ETHERTYPE_ATALK);
910 break;
911 #endif
912 #ifdef INET6
913 case AF_INET6:
914 etype = htons(ETHERTYPE_IPV6);
915 break;
916 #endif
917 default:
918 IF_DROP(&ifp->if_snd);
919 m_freem(m);
920 error = EAFNOSUPPORT;
921 goto end;
922 }
923
924 #ifdef MPLS
925 if (rt != NULL && rt_gettag(rt) != NULL) {
926 union mpls_shim msh;
927 msh.s_addr = MPLS_GETSADDR(rt);
928 if (msh.shim.label != MPLS_LABEL_IMPLNULL)
929 etype = htons(ETHERTYPE_MPLS);
930 }
931 #endif
932
933 M_PREPEND(m, sizeof(*gh), M_DONTWAIT);
934
935 if (m == NULL) {
936 IF_DROP(&ifp->if_snd);
937 error = ENOBUFS;
938 goto end;
939 }
940
941 gh = mtod(m, struct gre_h *);
942 gh->flags = 0;
943 gh->ptype = etype;
944 /* XXX Need to handle IP ToS. Look at how I handle IP TTL. */
945
946 ifp->if_opackets++;
947 ifp->if_obytes += m->m_pkthdr.len;
948
949 /* Clear checksum-offload flags. */
950 m->m_pkthdr.csum_flags = 0;
951 m->m_pkthdr.csum_data = 0;
952
953 /* send it off */
954 if ((error = gre_bufq_enqueue(&sc->sc_snd, m)) != 0) {
955 sc->sc_oflow_ev.ev_count++;
956 m_freem(m);
957 } else
958 softint_schedule(sc->sc_si);
959 end:
960 if (error)
961 ifp->if_oerrors++;
962 return error;
963 }
964
965 static int
966 gre_getname(struct socket *so, int req, struct mbuf *nam, struct lwp *l)
967 {
968 return (*so->so_proto->pr_usrreqs->pr_generic)(so,
969 req, NULL, nam, NULL, l);
970 }
971
972 static int
973 gre_getsockname(struct socket *so, struct mbuf *nam, struct lwp *l)
974 {
975 return gre_getname(so, PRU_SOCKADDR, nam, l);
976 }
977
978 static int
979 gre_getpeername(struct socket *so, struct mbuf *nam, struct lwp *l)
980 {
981 return gre_getname(so, PRU_PEERADDR, nam, l);
982 }
983
984 static int
985 gre_getnames(struct socket *so, struct lwp *l, struct sockaddr_storage *src,
986 struct sockaddr_storage *dst)
987 {
988 struct mbuf *m;
989 struct sockaddr_storage *ss;
990 int rc;
991
992 if ((m = getsombuf(so, MT_SONAME)) == NULL)
993 return ENOBUFS;
994
995 ss = mtod(m, struct sockaddr_storage *);
996
997 solock(so);
998 if ((rc = gre_getsockname(so, m, l)) != 0)
999 goto out;
1000 *src = *ss;
1001
1002 if ((rc = gre_getpeername(so, m, l)) != 0)
1003 goto out;
1004 *dst = *ss;
1005 out:
1006 sounlock(so);
1007 m_freem(m);
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 = (struct socket *)fp->f_data;
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 if_laddrreq *lifr = (struct if_laddrreq *)data;
1181 struct gre_softc *sc = ifp->if_softc;
1182 struct gre_soparm *sp;
1183 int fd, error = 0, oproto, otype, s;
1184 struct gre_soparm sp0;
1185
1186 ifr = data;
1187
1188 GRE_DPRINTF(sc, "cmd %lu\n", cmd);
1189
1190 switch (cmd) {
1191 case GRESPROTO:
1192 case GRESADDRD:
1193 case GRESADDRS:
1194 case GRESSOCK:
1195 case GREDSOCK:
1196 if (kauth_authorize_network(curlwp->l_cred,
1197 KAUTH_NETWORK_INTERFACE,
1198 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd,
1199 NULL) != 0)
1200 return EPERM;
1201 break;
1202 default:
1203 break;
1204 }
1205
1206 s = splnet();
1207
1208 sp0 = sc->sc_soparm;
1209 sp0.sp_so = NULL;
1210 sp = &sp0;
1211
1212 GRE_DPRINTF(sc, "\n");
1213
1214 switch (cmd) {
1215 case SIOCINITIFADDR:
1216 GRE_DPRINTF(sc, "\n");
1217 if ((ifp->if_flags & IFF_UP) != 0)
1218 break;
1219 gre_clearconf(sp, false);
1220 ifp->if_flags |= IFF_UP;
1221 goto mksocket;
1222 case SIOCSIFFLAGS:
1223 if ((error = ifioctl_common(ifp, cmd, data)) != 0)
1224 break;
1225 oproto = sp->sp_proto;
1226 otype = sp->sp_type;
1227 switch (ifr->ifr_flags & (IFF_LINK0|IFF_LINK2)) {
1228 case IFF_LINK0|IFF_LINK2:
1229 sp->sp_proto = IPPROTO_UDP;
1230 sp->sp_type = SOCK_DGRAM;
1231 break;
1232 case IFF_LINK2:
1233 sp->sp_proto = 0;
1234 sp->sp_type = 0;
1235 break;
1236 case IFF_LINK0:
1237 sp->sp_proto = IPPROTO_GRE;
1238 sp->sp_type = SOCK_RAW;
1239 break;
1240 default:
1241 GRE_DPRINTF(sc, "\n");
1242 error = EINVAL;
1243 goto out;
1244 }
1245 GRE_DPRINTF(sc, "\n");
1246 gre_clearconf(sp, false);
1247 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) ==
1248 (IFF_UP|IFF_RUNNING) &&
1249 (oproto == sp->sp_proto || sp->sp_proto == 0) &&
1250 (otype == sp->sp_type || sp->sp_type == 0))
1251 break;
1252 switch (sp->sp_proto) {
1253 case IPPROTO_UDP:
1254 case IPPROTO_GRE:
1255 goto mksocket;
1256 default:
1257 break;
1258 }
1259 break;
1260 case SIOCSIFMTU:
1261 /* XXX determine MTU automatically by probing w/
1262 * XXX do-not-fragment packets?
1263 */
1264 if (ifr->ifr_mtu < 576) {
1265 error = EINVAL;
1266 break;
1267 }
1268 /*FALLTHROUGH*/
1269 case SIOCGIFMTU:
1270 if ((error = ifioctl_common(ifp, cmd, data)) == ENETRESET)
1271 error = 0;
1272 break;
1273 case SIOCADDMULTI:
1274 case SIOCDELMULTI:
1275 if (ifr == NULL) {
1276 error = EAFNOSUPPORT;
1277 break;
1278 }
1279 switch (ifreq_getaddr(cmd, ifr)->sa_family) {
1280 #ifdef INET
1281 case AF_INET:
1282 break;
1283 #endif
1284 #ifdef INET6
1285 case AF_INET6:
1286 break;
1287 #endif
1288 default:
1289 error = EAFNOSUPPORT;
1290 break;
1291 }
1292 break;
1293 case GRESPROTO:
1294 gre_clearconf(sp, false);
1295 oproto = sp->sp_proto;
1296 otype = sp->sp_type;
1297 sp->sp_proto = ifr->ifr_flags;
1298 switch (sp->sp_proto) {
1299 case IPPROTO_UDP:
1300 ifp->if_flags |= IFF_LINK0|IFF_LINK2;
1301 sp->sp_type = SOCK_DGRAM;
1302 break;
1303 case IPPROTO_GRE:
1304 ifp->if_flags |= IFF_LINK0;
1305 ifp->if_flags &= ~IFF_LINK2;
1306 sp->sp_type = SOCK_RAW;
1307 break;
1308 case 0:
1309 ifp->if_flags &= ~IFF_LINK0;
1310 ifp->if_flags |= IFF_LINK2;
1311 sp->sp_type = 0;
1312 break;
1313 default:
1314 error = EPROTONOSUPPORT;
1315 break;
1316 }
1317 if ((oproto == sp->sp_proto || sp->sp_proto == 0) &&
1318 (otype == sp->sp_type || sp->sp_type == 0))
1319 break;
1320 switch (sp->sp_proto) {
1321 case IPPROTO_UDP:
1322 case IPPROTO_GRE:
1323 goto mksocket;
1324 default:
1325 break;
1326 }
1327 break;
1328 case GREGPROTO:
1329 ifr->ifr_flags = sp->sp_proto;
1330 break;
1331 case GRESADDRS:
1332 case GRESADDRD:
1333 gre_clearconf(sp, false);
1334 /* set tunnel endpoints and mark interface as up */
1335 switch (cmd) {
1336 case GRESADDRS:
1337 sockaddr_copy(sstosa(&sp->sp_src),
1338 sizeof(sp->sp_src), ifreq_getaddr(cmd, ifr));
1339 break;
1340 case GRESADDRD:
1341 sockaddr_copy(sstosa(&sp->sp_dst),
1342 sizeof(sp->sp_dst), ifreq_getaddr(cmd, ifr));
1343 break;
1344 }
1345 checkaddr:
1346 if (sockaddr_any(sstosa(&sp->sp_src)) == NULL ||
1347 sockaddr_any(sstosa(&sp->sp_dst)) == NULL) {
1348 error = EINVAL;
1349 break;
1350 }
1351 /* let gre_socreate() check the rest */
1352 mksocket:
1353 GRE_DPRINTF(sc, "\n");
1354 /* If we're administratively down, or the configuration
1355 * is empty, there's no use creating a socket.
1356 */
1357 if ((ifp->if_flags & IFF_UP) == 0 || gre_is_nullconf(sp))
1358 goto sendconf;
1359
1360 GRE_DPRINTF(sc, "\n");
1361 fd = 0;
1362 error = gre_socreate(sc, sp, &fd);
1363 if (error != 0)
1364 break;
1365
1366 setsock:
1367 GRE_DPRINTF(sc, "\n");
1368
1369 error = gre_ssock(ifp, sp, fd);
1370
1371 if (cmd != GRESSOCK) {
1372 GRE_DPRINTF(sc, "\n");
1373 /* XXX v. dodgy */
1374 if (fd_getfile(fd) != NULL)
1375 fd_close(fd);
1376 }
1377
1378 if (error == 0) {
1379 sendconf:
1380 GRE_DPRINTF(sc, "\n");
1381 ifp->if_flags &= ~IFF_RUNNING;
1382 gre_reconf(sc, sp);
1383 }
1384
1385 break;
1386 case GREGADDRS:
1387 ifreq_setaddr(cmd, ifr, sstosa(&sp->sp_src));
1388 break;
1389 case GREGADDRD:
1390 ifreq_setaddr(cmd, ifr, sstosa(&sp->sp_dst));
1391 break;
1392 case GREDSOCK:
1393 GRE_DPRINTF(sc, "\n");
1394 if (sp->sp_bysock)
1395 ifp->if_flags &= ~IFF_UP;
1396 gre_clearconf(sp, false);
1397 goto mksocket;
1398 case GRESSOCK:
1399 GRE_DPRINTF(sc, "\n");
1400 gre_clearconf(sp, true);
1401 fd = (int)ifr->ifr_value;
1402 sp->sp_bysock = true;
1403 ifp->if_flags |= IFF_UP;
1404 goto setsock;
1405 case SIOCSLIFPHYADDR:
1406 GRE_DPRINTF(sc, "\n");
1407 if (lifr->addr.ss_family != lifr->dstaddr.ss_family) {
1408 error = EAFNOSUPPORT;
1409 break;
1410 }
1411 sockaddr_copy(sstosa(&sp->sp_src), sizeof(sp->sp_src),
1412 sstosa(&lifr->addr));
1413 sockaddr_copy(sstosa(&sp->sp_dst), sizeof(sp->sp_dst),
1414 sstosa(&lifr->dstaddr));
1415 GRE_DPRINTF(sc, "\n");
1416 goto checkaddr;
1417 case SIOCDIFPHYADDR:
1418 GRE_DPRINTF(sc, "\n");
1419 gre_clearconf(sp, true);
1420 ifp->if_flags &= ~IFF_UP;
1421 goto mksocket;
1422 case SIOCGLIFPHYADDR:
1423 GRE_DPRINTF(sc, "\n");
1424 if (gre_is_nullconf(sp)) {
1425 error = EADDRNOTAVAIL;
1426 break;
1427 }
1428 sockaddr_copy(sstosa(&lifr->addr), sizeof(lifr->addr),
1429 sstosa(&sp->sp_src));
1430 sockaddr_copy(sstosa(&lifr->dstaddr), sizeof(lifr->dstaddr),
1431 sstosa(&sp->sp_dst));
1432 GRE_DPRINTF(sc, "\n");
1433 break;
1434 default:
1435 error = ifioctl_common(ifp, cmd, data);
1436 break;
1437 }
1438 out:
1439 GRE_DPRINTF(sc, "\n");
1440 splx(s);
1441 return error;
1442 }
1443
1444 void greattach(int);
1445
1446 /* ARGSUSED */
1447 void
1448 greattach(int count)
1449 {
1450 if_clone_attach(&gre_cloner);
1451 }
1452