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