if_gre.c revision 1.153 1 /* $NetBSD: if_gre.c,v 1.153 2013/11/07 21:44:48 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.153 2013/11/07 21:44:48 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 * 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, curlwp, &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_usrreq)(so, PRU_SEND, top, NULL, NULL, l);
532 top = NULL;
533 release:
534 sbunlock(&so->so_snd);
535 out:
536 sounlock(so);
537 if (top != NULL)
538 m_freem(top);
539 return error;
540 }
541
542 /* This is a stripped-down version of soreceive() that will never
543 * block. It will support SOCK_DGRAM sockets. It may also support
544 * SOCK_SEQPACKET sockets.
545 */
546 static int
547 gre_soreceive(struct socket *so, struct mbuf **mp0)
548 {
549 struct mbuf *m, **mp;
550 int flags, len, error, type;
551 const struct protosw *pr;
552 struct mbuf *nextrecord;
553
554 KASSERT(mp0 != NULL);
555
556 flags = MSG_DONTWAIT;
557 pr = so->so_proto;
558 mp = mp0;
559 type = 0;
560
561 *mp = NULL;
562
563 KASSERT(pr->pr_flags & PR_ATOMIC);
564 restart:
565 if ((error = sblock(&so->so_rcv, M_NOWAIT)) != 0) {
566 return error;
567 }
568 m = so->so_rcv.sb_mb;
569 /*
570 * If we have less data than requested, do not block awaiting more.
571 */
572 if (m == NULL) {
573 #ifdef DIAGNOSTIC
574 if (so->so_rcv.sb_cc)
575 panic("receive 1");
576 #endif
577 if (so->so_error) {
578 error = so->so_error;
579 so->so_error = 0;
580 } else if (so->so_state & SS_CANTRCVMORE)
581 ;
582 else if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0
583 && (so->so_proto->pr_flags & PR_CONNREQUIRED))
584 error = ENOTCONN;
585 else
586 error = EWOULDBLOCK;
587 goto release;
588 }
589 /*
590 * On entry here, m points to the first record of the socket buffer.
591 * While we process the initial mbufs containing address and control
592 * info, we save a copy of m->m_nextpkt into nextrecord.
593 */
594 if (curlwp != NULL)
595 curlwp->l_ru.ru_msgrcv++;
596 KASSERT(m == so->so_rcv.sb_mb);
597 SBLASTRECORDCHK(&so->so_rcv, "soreceive 1");
598 SBLASTMBUFCHK(&so->so_rcv, "soreceive 1");
599 nextrecord = m->m_nextpkt;
600 if (pr->pr_flags & PR_ADDR) {
601 #ifdef DIAGNOSTIC
602 if (m->m_type != MT_SONAME)
603 panic("receive 1a");
604 #endif
605 sbfree(&so->so_rcv, m);
606 MFREE(m, so->so_rcv.sb_mb);
607 m = so->so_rcv.sb_mb;
608 }
609 while (m != NULL && m->m_type == MT_CONTROL && error == 0) {
610 sbfree(&so->so_rcv, m);
611 /*
612 * Dispose of any SCM_RIGHTS message that went
613 * through the read path rather than recv.
614 */
615 if (pr->pr_domain->dom_dispose &&
616 mtod(m, struct cmsghdr *)->cmsg_type == SCM_RIGHTS)
617 (*pr->pr_domain->dom_dispose)(m);
618 MFREE(m, so->so_rcv.sb_mb);
619 m = so->so_rcv.sb_mb;
620 }
621
622 /*
623 * If m is non-NULL, we have some data to read. From now on,
624 * make sure to keep sb_lastrecord consistent when working on
625 * the last packet on the chain (nextrecord == NULL) and we
626 * change m->m_nextpkt.
627 */
628 if (m != NULL) {
629 m->m_nextpkt = nextrecord;
630 /*
631 * If nextrecord == NULL (this is a single chain),
632 * then sb_lastrecord may not be valid here if m
633 * was changed earlier.
634 */
635 if (nextrecord == NULL) {
636 KASSERT(so->so_rcv.sb_mb == m);
637 so->so_rcv.sb_lastrecord = m;
638 }
639 type = m->m_type;
640 if (type == MT_OOBDATA)
641 flags |= MSG_OOB;
642 } else {
643 KASSERT(so->so_rcv.sb_mb == m);
644 so->so_rcv.sb_mb = nextrecord;
645 SB_EMPTY_FIXUP(&so->so_rcv);
646 }
647 SBLASTRECORDCHK(&so->so_rcv, "soreceive 2");
648 SBLASTMBUFCHK(&so->so_rcv, "soreceive 2");
649
650 while (m != NULL) {
651 if (m->m_type == MT_OOBDATA) {
652 if (type != MT_OOBDATA)
653 break;
654 } else if (type == MT_OOBDATA)
655 break;
656 #ifdef DIAGNOSTIC
657 else if (m->m_type != MT_DATA && m->m_type != MT_HEADER)
658 panic("receive 3");
659 #endif
660 so->so_state &= ~SS_RCVATMARK;
661 if (so->so_oobmark != 0 && so->so_oobmark < m->m_len)
662 break;
663 len = m->m_len;
664 /*
665 * mp is set, just pass back the mbufs.
666 * Sockbuf must be consistent here (points to current mbuf,
667 * it points to next record) when we drop priority;
668 * we must note any additions to the sockbuf when we
669 * block interrupts again.
670 */
671 if (m->m_flags & M_EOR)
672 flags |= MSG_EOR;
673 nextrecord = m->m_nextpkt;
674 sbfree(&so->so_rcv, m);
675 *mp = m;
676 mp = &m->m_next;
677 so->so_rcv.sb_mb = m = m->m_next;
678 *mp = NULL;
679 /*
680 * If m != NULL, we also know that
681 * so->so_rcv.sb_mb != NULL.
682 */
683 KASSERT(so->so_rcv.sb_mb == m);
684 if (m) {
685 m->m_nextpkt = nextrecord;
686 if (nextrecord == NULL)
687 so->so_rcv.sb_lastrecord = m;
688 } else {
689 so->so_rcv.sb_mb = nextrecord;
690 SB_EMPTY_FIXUP(&so->so_rcv);
691 }
692 SBLASTRECORDCHK(&so->so_rcv, "soreceive 3");
693 SBLASTMBUFCHK(&so->so_rcv, "soreceive 3");
694 if (so->so_oobmark) {
695 so->so_oobmark -= len;
696 if (so->so_oobmark == 0) {
697 so->so_state |= SS_RCVATMARK;
698 break;
699 }
700 }
701 if (flags & MSG_EOR)
702 break;
703 }
704
705 if (m != NULL) {
706 m_freem(*mp);
707 *mp = NULL;
708 error = ENOMEM;
709 (void) sbdroprecord(&so->so_rcv);
710 } else {
711 /*
712 * First part is an inline SB_EMPTY_FIXUP(). Second
713 * part makes sure sb_lastrecord is up-to-date if
714 * there is still data in the socket buffer.
715 */
716 so->so_rcv.sb_mb = nextrecord;
717 if (so->so_rcv.sb_mb == NULL) {
718 so->so_rcv.sb_mbtail = NULL;
719 so->so_rcv.sb_lastrecord = NULL;
720 } else if (nextrecord->m_nextpkt == NULL)
721 so->so_rcv.sb_lastrecord = nextrecord;
722 }
723 SBLASTRECORDCHK(&so->so_rcv, "soreceive 4");
724 SBLASTMBUFCHK(&so->so_rcv, "soreceive 4");
725 if (pr->pr_flags & PR_WANTRCVD && so->so_pcb)
726 (*pr->pr_usrreq)(so, PRU_RCVD, NULL,
727 (struct mbuf *)(long)flags, NULL, curlwp);
728 if (*mp0 == NULL && (flags & MSG_EOR) == 0 &&
729 (so->so_state & SS_CANTRCVMORE) == 0) {
730 sbunlock(&so->so_rcv);
731 goto restart;
732 }
733
734 release:
735 sbunlock(&so->so_rcv);
736 return error;
737 }
738
739 static struct socket *
740 gre_reconf(struct gre_softc *sc, const struct gre_soparm *newsoparm)
741 {
742 struct ifnet *ifp = &sc->sc_if;
743
744 GRE_DPRINTF(sc, "enter\n");
745
746 shutdown:
747 if (sc->sc_soparm.sp_so != NULL) {
748 GRE_DPRINTF(sc, "\n");
749 gre_upcall_remove(sc->sc_soparm.sp_so);
750 softint_disestablish(sc->sc_si);
751 sc->sc_si = NULL;
752 gre_fp_send(sc, GRE_M_DELFP, NULL);
753 gre_clearconf(&sc->sc_soparm, false);
754 }
755
756 if (newsoparm != NULL) {
757 GRE_DPRINTF(sc, "\n");
758 sc->sc_soparm = *newsoparm;
759 newsoparm = NULL;
760 }
761
762 if (sc->sc_soparm.sp_so != NULL) {
763 GRE_DPRINTF(sc, "\n");
764 sc->sc_si = softint_establish(SOFTINT_NET, greintr, sc);
765 gre_upcall_add(sc->sc_soparm.sp_so, sc);
766 if ((ifp->if_flags & IFF_UP) == 0) {
767 GRE_DPRINTF(sc, "down\n");
768 goto shutdown;
769 }
770 }
771
772 GRE_DPRINTF(sc, "\n");
773 if (sc->sc_soparm.sp_so != NULL)
774 sc->sc_if.if_flags |= IFF_RUNNING;
775 else {
776 gre_bufq_purge(&sc->sc_snd);
777 sc->sc_if.if_flags &= ~IFF_RUNNING;
778 }
779 return sc->sc_soparm.sp_so;
780 }
781
782 static int
783 gre_input(struct gre_softc *sc, struct mbuf *m, int hlen,
784 const struct gre_h *gh)
785 {
786 uint16_t flags;
787 uint32_t af; /* af passed to BPF tap */
788 int isr, s;
789 struct ifqueue *ifq;
790
791 sc->sc_if.if_ipackets++;
792 sc->sc_if.if_ibytes += m->m_pkthdr.len;
793
794 hlen += sizeof(struct gre_h);
795
796 /* process GRE flags as packet can be of variable len */
797 flags = ntohs(gh->flags);
798
799 /* Checksum & Offset are present */
800 if ((flags & GRE_CP) | (flags & GRE_RP))
801 hlen += 4;
802 /* We don't support routing fields (variable length) */
803 if (flags & GRE_RP) {
804 sc->sc_if.if_ierrors++;
805 return 0;
806 }
807 if (flags & GRE_KP)
808 hlen += 4;
809 if (flags & GRE_SP)
810 hlen += 4;
811
812 switch (ntohs(gh->ptype)) { /* ethertypes */
813 #ifdef INET
814 case ETHERTYPE_IP:
815 ifq = &ipintrq;
816 isr = NETISR_IP;
817 af = AF_INET;
818 break;
819 #endif
820 #ifdef NETATALK
821 case ETHERTYPE_ATALK:
822 ifq = &atintrq1;
823 isr = NETISR_ATALK;
824 af = AF_APPLETALK;
825 break;
826 #endif
827 #ifdef INET6
828 case ETHERTYPE_IPV6:
829 ifq = &ip6intrq;
830 isr = NETISR_IPV6;
831 af = AF_INET6;
832 break;
833 #endif
834 #ifdef MPLS
835 case ETHERTYPE_MPLS:
836 ifq = &mplsintrq;
837 isr = NETISR_MPLS;
838 af = AF_MPLS;
839 break;
840 #endif
841 default: /* others not yet supported */
842 GRE_DPRINTF(sc, "unhandled ethertype 0x%04x\n",
843 ntohs(gh->ptype));
844 sc->sc_if.if_noproto++;
845 return 0;
846 }
847
848 if (hlen > m->m_pkthdr.len) {
849 m_freem(m);
850 sc->sc_if.if_ierrors++;
851 return EINVAL;
852 }
853 m_adj(m, hlen);
854
855 bpf_mtap_af(&sc->sc_if, af, m);
856
857 m->m_pkthdr.rcvif = &sc->sc_if;
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_getname(struct socket *so, int req, struct mbuf *nam, struct lwp *l)
966 {
967 return (*so->so_proto->pr_usrreq)(so, req, NULL, nam, NULL, l);
968 }
969
970 static int
971 gre_getsockname(struct socket *so, struct mbuf *nam, struct lwp *l)
972 {
973 return gre_getname(so, PRU_SOCKADDR, nam, l);
974 }
975
976 static int
977 gre_getpeername(struct socket *so, struct mbuf *nam, struct lwp *l)
978 {
979 return gre_getname(so, PRU_PEERADDR, nam, l);
980 }
981
982 static int
983 gre_getnames(struct socket *so, struct lwp *l, struct sockaddr_storage *src,
984 struct sockaddr_storage *dst)
985 {
986 struct mbuf *m;
987 struct sockaddr_storage *ss;
988 int rc;
989
990 if ((m = getsombuf(so, MT_SONAME)) == NULL)
991 return ENOBUFS;
992
993 ss = mtod(m, struct sockaddr_storage *);
994
995 solock(so);
996 if ((rc = gre_getsockname(so, m, l)) != 0)
997 goto out;
998 *src = *ss;
999
1000 if ((rc = gre_getpeername(so, m, l)) != 0)
1001 goto out;
1002 *dst = *ss;
1003 out:
1004 sounlock(so);
1005 m_freem(m);
1006 return rc;
1007 }
1008
1009 static void
1010 gre_fp_recvloop(void *arg)
1011 {
1012 struct gre_softc *sc = arg;
1013
1014 mutex_enter(&sc->sc_mtx);
1015 while (gre_fp_recv(sc))
1016 ;
1017 mutex_exit(&sc->sc_mtx);
1018 kthread_exit(0);
1019 }
1020
1021 static bool
1022 gre_fp_recv(struct gre_softc *sc)
1023 {
1024 int fd, ofd, rc;
1025 file_t *fp;
1026
1027 fp = sc->sc_fp;
1028 ofd = sc->sc_fd;
1029 fd = -1;
1030
1031 switch (sc->sc_msg) {
1032 case GRE_M_STOP:
1033 cv_signal(&sc->sc_fp_condvar);
1034 return false;
1035 case GRE_M_SETFP:
1036 mutex_exit(&sc->sc_mtx);
1037 rc = fd_dup(fp, 0, &fd, 0);
1038 mutex_enter(&sc->sc_mtx);
1039 if (rc != 0) {
1040 sc->sc_msg = GRE_M_ERR;
1041 break;
1042 }
1043 /*FALLTHROUGH*/
1044 case GRE_M_DELFP:
1045 mutex_exit(&sc->sc_mtx);
1046 if (ofd != -1 && fd_getfile(ofd) != NULL)
1047 fd_close(ofd);
1048 mutex_enter(&sc->sc_mtx);
1049 sc->sc_fd = fd;
1050 sc->sc_msg = GRE_M_OK;
1051 break;
1052 default:
1053 gre_fp_wait(sc);
1054 return true;
1055 }
1056 cv_signal(&sc->sc_fp_condvar);
1057 return true;
1058 }
1059
1060 static bool
1061 gre_fp_send(struct gre_softc *sc, enum gre_msg msg, file_t *fp)
1062 {
1063 bool rc;
1064
1065 mutex_enter(&sc->sc_mtx);
1066 while (sc->sc_msg != GRE_M_NONE)
1067 gre_fp_wait(sc);
1068 sc->sc_fp = fp;
1069 sc->sc_msg = msg;
1070 cv_signal(&sc->sc_fp_condvar);
1071 while (sc->sc_msg != GRE_M_STOP && sc->sc_msg != GRE_M_OK &&
1072 sc->sc_msg != GRE_M_ERR)
1073 gre_fp_wait(sc);
1074 rc = (sc->sc_msg != GRE_M_ERR);
1075 sc->sc_msg = GRE_M_NONE;
1076 cv_signal(&sc->sc_fp_condvar);
1077 mutex_exit(&sc->sc_mtx);
1078 return rc;
1079 }
1080
1081 static int
1082 gre_ssock(struct ifnet *ifp, struct gre_soparm *sp, int fd)
1083 {
1084 int error = 0;
1085 const struct protosw *pr;
1086 file_t *fp;
1087 struct gre_softc *sc = ifp->if_softc;
1088 struct socket *so;
1089 struct sockaddr_storage dst, src;
1090
1091 if ((fp = fd_getfile(fd)) == NULL)
1092 return EBADF;
1093 if (fp->f_type != DTYPE_SOCKET) {
1094 fd_putfile(fd);
1095 return ENOTSOCK;
1096 }
1097
1098 GRE_DPRINTF(sc, "\n");
1099
1100 so = (struct socket *)fp->f_data;
1101 pr = so->so_proto;
1102
1103 GRE_DPRINTF(sc, "type %d, proto %d\n", pr->pr_type, pr->pr_protocol);
1104
1105 if ((pr->pr_flags & PR_ATOMIC) == 0 ||
1106 (sp->sp_type != 0 && pr->pr_type != sp->sp_type) ||
1107 (sp->sp_proto != 0 && pr->pr_protocol != 0 &&
1108 pr->pr_protocol != sp->sp_proto)) {
1109 error = EINVAL;
1110 goto err;
1111 }
1112
1113 GRE_DPRINTF(sc, "\n");
1114
1115 /* check address */
1116 if ((error = gre_getnames(so, curlwp, &src, &dst)) != 0)
1117 goto err;
1118
1119 GRE_DPRINTF(sc, "\n");
1120
1121 if (!gre_fp_send(sc, GRE_M_SETFP, fp)) {
1122 error = EBUSY;
1123 goto err;
1124 }
1125
1126 GRE_DPRINTF(sc, "\n");
1127
1128 sp->sp_src = src;
1129 sp->sp_dst = dst;
1130
1131 sp->sp_so = so;
1132
1133 err:
1134 fd_putfile(fd);
1135 return error;
1136 }
1137
1138 static bool
1139 sockaddr_is_anyaddr(const struct sockaddr *sa)
1140 {
1141 socklen_t anylen, salen;
1142 const void *anyaddr, *addr;
1143
1144 if ((anyaddr = sockaddr_anyaddr(sa, &anylen)) == NULL ||
1145 (addr = sockaddr_const_addr(sa, &salen)) == NULL)
1146 return false;
1147
1148 if (salen > anylen)
1149 return false;
1150
1151 return memcmp(anyaddr, addr, MIN(anylen, salen)) == 0;
1152 }
1153
1154 static bool
1155 gre_is_nullconf(const struct gre_soparm *sp)
1156 {
1157 return sockaddr_is_anyaddr(sstocsa(&sp->sp_src)) ||
1158 sockaddr_is_anyaddr(sstocsa(&sp->sp_dst));
1159 }
1160
1161 static void
1162 gre_clearconf(struct gre_soparm *sp, bool force)
1163 {
1164 if (sp->sp_bysock || force) {
1165 sockaddr_copy(sstosa(&sp->sp_src), sizeof(sp->sp_src),
1166 sockaddr_any(sstosa(&sp->sp_src)));
1167 sockaddr_copy(sstosa(&sp->sp_dst), sizeof(sp->sp_dst),
1168 sockaddr_any(sstosa(&sp->sp_dst)));
1169 sp->sp_bysock = false;
1170 }
1171 sp->sp_so = NULL; /* XXX */
1172 }
1173
1174 static int
1175 gre_ioctl(struct ifnet *ifp, const u_long cmd, void *data)
1176 {
1177 struct ifreq *ifr;
1178 struct if_laddrreq *lifr = (struct if_laddrreq *)data;
1179 struct gre_softc *sc = ifp->if_softc;
1180 struct gre_soparm *sp;
1181 int fd, error = 0, oproto, otype, s;
1182 struct gre_soparm sp0;
1183
1184 ifr = data;
1185
1186 GRE_DPRINTF(sc, "cmd %lu\n", cmd);
1187
1188 switch (cmd) {
1189 case GRESPROTO:
1190 case GRESADDRD:
1191 case GRESADDRS:
1192 case GRESSOCK:
1193 case GREDSOCK:
1194 if (kauth_authorize_network(curlwp->l_cred,
1195 KAUTH_NETWORK_INTERFACE,
1196 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd,
1197 NULL) != 0)
1198 return EPERM;
1199 break;
1200 default:
1201 break;
1202 }
1203
1204 s = splnet();
1205
1206 sp0 = sc->sc_soparm;
1207 sp0.sp_so = NULL;
1208 sp = &sp0;
1209
1210 GRE_DPRINTF(sc, "\n");
1211
1212 switch (cmd) {
1213 case SIOCINITIFADDR:
1214 GRE_DPRINTF(sc, "\n");
1215 if ((ifp->if_flags & IFF_UP) != 0)
1216 break;
1217 gre_clearconf(sp, false);
1218 ifp->if_flags |= IFF_UP;
1219 goto mksocket;
1220 case SIOCSIFFLAGS:
1221 if ((error = ifioctl_common(ifp, cmd, data)) != 0)
1222 break;
1223 oproto = sp->sp_proto;
1224 otype = sp->sp_type;
1225 switch (ifr->ifr_flags & (IFF_LINK0|IFF_LINK2)) {
1226 case IFF_LINK0|IFF_LINK2:
1227 sp->sp_proto = IPPROTO_UDP;
1228 sp->sp_type = SOCK_DGRAM;
1229 break;
1230 case IFF_LINK2:
1231 sp->sp_proto = 0;
1232 sp->sp_type = 0;
1233 break;
1234 case IFF_LINK0:
1235 sp->sp_proto = IPPROTO_GRE;
1236 sp->sp_type = SOCK_RAW;
1237 break;
1238 default:
1239 GRE_DPRINTF(sc, "\n");
1240 error = EINVAL;
1241 goto out;
1242 }
1243 GRE_DPRINTF(sc, "\n");
1244 gre_clearconf(sp, false);
1245 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) ==
1246 (IFF_UP|IFF_RUNNING) &&
1247 (oproto == sp->sp_proto || sp->sp_proto == 0) &&
1248 (otype == sp->sp_type || sp->sp_type == 0))
1249 break;
1250 switch (sp->sp_proto) {
1251 case IPPROTO_UDP:
1252 case IPPROTO_GRE:
1253 goto mksocket;
1254 default:
1255 break;
1256 }
1257 break;
1258 case SIOCSIFMTU:
1259 /* XXX determine MTU automatically by probing w/
1260 * XXX do-not-fragment packets?
1261 */
1262 if (ifr->ifr_mtu < 576) {
1263 error = EINVAL;
1264 break;
1265 }
1266 /*FALLTHROUGH*/
1267 case SIOCGIFMTU:
1268 if ((error = ifioctl_common(ifp, cmd, data)) == ENETRESET)
1269 error = 0;
1270 break;
1271 case SIOCADDMULTI:
1272 case SIOCDELMULTI:
1273 if (ifr == NULL) {
1274 error = EAFNOSUPPORT;
1275 break;
1276 }
1277 switch (ifreq_getaddr(cmd, ifr)->sa_family) {
1278 #ifdef INET
1279 case AF_INET:
1280 break;
1281 #endif
1282 #ifdef INET6
1283 case AF_INET6:
1284 break;
1285 #endif
1286 default:
1287 error = EAFNOSUPPORT;
1288 break;
1289 }
1290 break;
1291 case GRESPROTO:
1292 gre_clearconf(sp, false);
1293 oproto = sp->sp_proto;
1294 otype = sp->sp_type;
1295 sp->sp_proto = ifr->ifr_flags;
1296 switch (sp->sp_proto) {
1297 case IPPROTO_UDP:
1298 ifp->if_flags |= IFF_LINK0|IFF_LINK2;
1299 sp->sp_type = SOCK_DGRAM;
1300 break;
1301 case IPPROTO_GRE:
1302 ifp->if_flags |= IFF_LINK0;
1303 ifp->if_flags &= ~IFF_LINK2;
1304 sp->sp_type = SOCK_RAW;
1305 break;
1306 case 0:
1307 ifp->if_flags &= ~IFF_LINK0;
1308 ifp->if_flags |= IFF_LINK2;
1309 sp->sp_type = 0;
1310 break;
1311 default:
1312 error = EPROTONOSUPPORT;
1313 break;
1314 }
1315 if ((oproto == sp->sp_proto || sp->sp_proto == 0) &&
1316 (otype == sp->sp_type || sp->sp_type == 0))
1317 break;
1318 switch (sp->sp_proto) {
1319 case IPPROTO_UDP:
1320 case IPPROTO_GRE:
1321 goto mksocket;
1322 default:
1323 break;
1324 }
1325 break;
1326 case GREGPROTO:
1327 ifr->ifr_flags = sp->sp_proto;
1328 break;
1329 case GRESADDRS:
1330 case GRESADDRD:
1331 gre_clearconf(sp, false);
1332 /* set tunnel endpoints and mark interface as up */
1333 switch (cmd) {
1334 case GRESADDRS:
1335 sockaddr_copy(sstosa(&sp->sp_src),
1336 sizeof(sp->sp_src), ifreq_getaddr(cmd, ifr));
1337 break;
1338 case GRESADDRD:
1339 sockaddr_copy(sstosa(&sp->sp_dst),
1340 sizeof(sp->sp_dst), ifreq_getaddr(cmd, ifr));
1341 break;
1342 }
1343 checkaddr:
1344 if (sockaddr_any(sstosa(&sp->sp_src)) == NULL ||
1345 sockaddr_any(sstosa(&sp->sp_dst)) == NULL) {
1346 error = EINVAL;
1347 break;
1348 }
1349 /* let gre_socreate() check the rest */
1350 mksocket:
1351 GRE_DPRINTF(sc, "\n");
1352 /* If we're administratively down, or the configuration
1353 * is empty, there's no use creating a socket.
1354 */
1355 if ((ifp->if_flags & IFF_UP) == 0 || gre_is_nullconf(sp))
1356 goto sendconf;
1357
1358 GRE_DPRINTF(sc, "\n");
1359 fd = 0;
1360 error = gre_socreate(sc, sp, &fd);
1361 if (error != 0)
1362 break;
1363
1364 setsock:
1365 GRE_DPRINTF(sc, "\n");
1366
1367 error = gre_ssock(ifp, sp, fd);
1368
1369 if (cmd != GRESSOCK) {
1370 GRE_DPRINTF(sc, "\n");
1371 /* XXX v. dodgy */
1372 if (fd_getfile(fd) != NULL)
1373 fd_close(fd);
1374 }
1375
1376 if (error == 0) {
1377 sendconf:
1378 GRE_DPRINTF(sc, "\n");
1379 ifp->if_flags &= ~IFF_RUNNING;
1380 gre_reconf(sc, sp);
1381 }
1382
1383 break;
1384 case GREGADDRS:
1385 ifreq_setaddr(cmd, ifr, sstosa(&sp->sp_src));
1386 break;
1387 case GREGADDRD:
1388 ifreq_setaddr(cmd, ifr, sstosa(&sp->sp_dst));
1389 break;
1390 case GREDSOCK:
1391 GRE_DPRINTF(sc, "\n");
1392 if (sp->sp_bysock)
1393 ifp->if_flags &= ~IFF_UP;
1394 gre_clearconf(sp, false);
1395 goto mksocket;
1396 case GRESSOCK:
1397 GRE_DPRINTF(sc, "\n");
1398 gre_clearconf(sp, true);
1399 fd = (int)ifr->ifr_value;
1400 sp->sp_bysock = true;
1401 ifp->if_flags |= IFF_UP;
1402 goto setsock;
1403 case SIOCSLIFPHYADDR:
1404 GRE_DPRINTF(sc, "\n");
1405 if (lifr->addr.ss_family != lifr->dstaddr.ss_family) {
1406 error = EAFNOSUPPORT;
1407 break;
1408 }
1409 sockaddr_copy(sstosa(&sp->sp_src), sizeof(sp->sp_src),
1410 sstosa(&lifr->addr));
1411 sockaddr_copy(sstosa(&sp->sp_dst), sizeof(sp->sp_dst),
1412 sstosa(&lifr->dstaddr));
1413 GRE_DPRINTF(sc, "\n");
1414 goto checkaddr;
1415 case SIOCDIFPHYADDR:
1416 GRE_DPRINTF(sc, "\n");
1417 gre_clearconf(sp, true);
1418 ifp->if_flags &= ~IFF_UP;
1419 goto mksocket;
1420 case SIOCGLIFPHYADDR:
1421 GRE_DPRINTF(sc, "\n");
1422 if (gre_is_nullconf(sp)) {
1423 error = EADDRNOTAVAIL;
1424 break;
1425 }
1426 sockaddr_copy(sstosa(&lifr->addr), sizeof(lifr->addr),
1427 sstosa(&sp->sp_src));
1428 sockaddr_copy(sstosa(&lifr->dstaddr), sizeof(lifr->dstaddr),
1429 sstosa(&sp->sp_dst));
1430 GRE_DPRINTF(sc, "\n");
1431 break;
1432 default:
1433 error = ifioctl_common(ifp, cmd, data);
1434 break;
1435 }
1436 out:
1437 GRE_DPRINTF(sc, "\n");
1438 splx(s);
1439 return error;
1440 }
1441
1442 void greattach(int);
1443
1444 /* ARGSUSED */
1445 void
1446 greattach(int count)
1447 {
1448 if_clone_attach(&gre_cloner);
1449 }
1450