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