if_gre.c revision 1.129 1 /* $NetBSD: if_gre.c,v 1.129 2008/04/03 21:40:59 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.129 2008/04/03 21:40:59 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 badsock:
828 gre_clearconf(&sc->sc_soparm, false);
829 so = NULL;
830 }
831
832 if (newsoparm != NULL) {
833 GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
834 sc->sc_soparm = *newsoparm;
835 newsoparm = NULL;
836 }
837
838 if (sc->sc_soparm.sp_fd != -1) {
839 GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
840 rc = getsock(sc->sc_soparm.sp_fd, &fp);
841 if (rc != 0)
842 goto badsock;
843 GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
844 so = (struct socket *)fp->f_data;
845 sc->sc_si = softint_establish(SOFTINT_NET, greintr, sc);
846 gre_upcall_add(so, sc);
847 fd_putfile(sc->sc_soparm.sp_fd);
848 if ((ifp->if_flags & IFF_UP) == 0) {
849 GRE_DPRINTF(sc, "%s: down\n", __func__);
850 goto shutdown;
851 }
852 }
853
854 GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
855 if (so != NULL)
856 sc->sc_if.if_flags |= IFF_RUNNING;
857 else {
858 gre_bufq_purge(&sc->sc_snd);
859 sc->sc_if.if_flags &= ~IFF_RUNNING;
860 }
861 return so;
862 }
863
864 static int
865 gre_input(struct gre_softc *sc, struct mbuf *m, int hlen,
866 const struct gre_h *gh)
867 {
868 uint16_t flags;
869 uint32_t af; /* af passed to BPF tap */
870 int isr, s;
871 struct ifqueue *ifq;
872
873 sc->sc_if.if_ipackets++;
874 sc->sc_if.if_ibytes += m->m_pkthdr.len;
875
876 hlen += sizeof(struct gre_h);
877
878 /* process GRE flags as packet can be of variable len */
879 flags = ntohs(gh->flags);
880
881 /* Checksum & Offset are present */
882 if ((flags & GRE_CP) | (flags & GRE_RP))
883 hlen += 4;
884 /* We don't support routing fields (variable length) */
885 if (flags & GRE_RP) {
886 sc->sc_if.if_ierrors++;
887 return 0;
888 }
889 if (flags & GRE_KP)
890 hlen += 4;
891 if (flags & GRE_SP)
892 hlen += 4;
893
894 switch (ntohs(gh->ptype)) { /* ethertypes */
895 case ETHERTYPE_IP:
896 ifq = &ipintrq;
897 isr = NETISR_IP;
898 af = AF_INET;
899 break;
900 #ifdef NETATALK
901 case ETHERTYPE_ATALK:
902 ifq = &atintrq1;
903 isr = NETISR_ATALK;
904 af = AF_APPLETALK;
905 break;
906 #endif
907 #ifdef INET6
908 case ETHERTYPE_IPV6:
909 ifq = &ip6intrq;
910 isr = NETISR_IPV6;
911 af = AF_INET6;
912 break;
913 #endif
914 default: /* others not yet supported */
915 GRE_DPRINTF(sc, "%s: unhandled ethertype 0x%04x\n", __func__,
916 ntohs(gh->ptype));
917 sc->sc_if.if_noproto++;
918 return 0;
919 }
920
921 if (hlen > m->m_pkthdr.len) {
922 m_freem(m);
923 sc->sc_if.if_ierrors++;
924 return EINVAL;
925 }
926 m_adj(m, hlen);
927
928 #if NBPFILTER > 0
929 if (sc->sc_if.if_bpf != NULL)
930 bpf_mtap_af(sc->sc_if.if_bpf, af, m);
931 #endif /*NBPFILTER > 0*/
932
933 m->m_pkthdr.rcvif = &sc->sc_if;
934
935 s = splnet();
936 if (IF_QFULL(ifq)) {
937 IF_DROP(ifq);
938 m_freem(m);
939 } else {
940 IF_ENQUEUE(ifq, m);
941 }
942 /* we need schednetisr since the address family may change */
943 schednetisr(isr);
944 splx(s);
945
946 return 1; /* packet is done, no further processing needed */
947 }
948
949 /*
950 * The output routine. Takes a packet and encapsulates it in the protocol
951 * given by sc->sc_soparm.sp_proto. See also RFC 1701 and RFC 2004
952 */
953 static int
954 gre_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
955 struct rtentry *rt)
956 {
957 int error = 0;
958 struct gre_softc *sc = ifp->if_softc;
959 struct gre_h *gh;
960 struct ip *ip;
961 uint8_t ip_tos = 0;
962 uint16_t etype = 0;
963
964 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) {
965 m_freem(m);
966 error = ENETDOWN;
967 goto end;
968 }
969
970 #if NBPFILTER > 0
971 if (ifp->if_bpf != NULL)
972 bpf_mtap_af(ifp->if_bpf, dst->sa_family, m);
973 #endif
974
975 m->m_flags &= ~(M_BCAST|M_MCAST);
976
977 GRE_DPRINTF(sc, "%s: dst->sa_family=%d\n", __func__, dst->sa_family);
978 switch (dst->sa_family) {
979 case AF_INET:
980 ip = mtod(m, struct ip *);
981 ip_tos = ip->ip_tos;
982 etype = htons(ETHERTYPE_IP);
983 break;
984 #ifdef NETATALK
985 case AF_APPLETALK:
986 etype = htons(ETHERTYPE_ATALK);
987 break;
988 #endif
989 #ifdef INET6
990 case AF_INET6:
991 etype = htons(ETHERTYPE_IPV6);
992 break;
993 #endif
994 default:
995 IF_DROP(&ifp->if_snd);
996 m_freem(m);
997 error = EAFNOSUPPORT;
998 goto end;
999 }
1000
1001 M_PREPEND(m, sizeof(*gh), M_DONTWAIT);
1002
1003 if (m == NULL) {
1004 IF_DROP(&ifp->if_snd);
1005 error = ENOBUFS;
1006 goto end;
1007 }
1008
1009 gh = mtod(m, struct gre_h *);
1010 gh->flags = 0;
1011 gh->ptype = etype;
1012 /* XXX Need to handle IP ToS. Look at how I handle IP TTL. */
1013
1014 ifp->if_opackets++;
1015 ifp->if_obytes += m->m_pkthdr.len;
1016
1017 /* send it off */
1018 if ((error = gre_bufq_enqueue(&sc->sc_snd, m)) != 0) {
1019 sc->sc_oflow_ev.ev_count++;
1020 m_freem(m);
1021 } else
1022 softint_schedule(sc->sc_si);
1023 end:
1024 if (error)
1025 ifp->if_oerrors++;
1026 return error;
1027 }
1028
1029 static int
1030 gre_getname(struct socket *so, int req, struct mbuf *nam, struct lwp *l)
1031 {
1032 return (*so->so_proto->pr_usrreq)(so, req, NULL, nam, NULL, l);
1033 }
1034
1035 static int
1036 gre_getsockname(struct socket *so, struct mbuf *nam, struct lwp *l)
1037 {
1038 return gre_getname(so, PRU_SOCKADDR, nam, l);
1039 }
1040
1041 static int
1042 gre_getpeername(struct socket *so, struct mbuf *nam, struct lwp *l)
1043 {
1044 return gre_getname(so, PRU_PEERADDR, nam, l);
1045 }
1046
1047 static int
1048 gre_getnames(struct socket *so, struct lwp *l, struct sockaddr_storage *src,
1049 struct sockaddr_storage *dst)
1050 {
1051 struct mbuf *m;
1052 struct sockaddr_storage *ss;
1053 int rc;
1054
1055 if ((m = getsombuf(so, MT_SONAME)) == NULL)
1056 return ENOBUFS;
1057
1058 ss = mtod(m, struct sockaddr_storage *);
1059
1060 if ((rc = gre_getsockname(so, m, l)) != 0)
1061 goto out;
1062 *src = *ss;
1063
1064 if ((rc = gre_getpeername(so, m, l)) != 0)
1065 goto out;
1066 *dst = *ss;
1067
1068 out:
1069 m_freem(m);
1070 return rc;
1071 }
1072
1073 static void
1074 gre_closef(file_t **fpp)
1075 {
1076 file_t *fp = *fpp;
1077
1078 closef(fp);
1079 *fpp = NULL;
1080 }
1081
1082 static int
1083 gre_ssock(struct ifnet *ifp, struct gre_soparm *sp, int fd)
1084 {
1085 int error, kfd;
1086 const struct protosw *pr;
1087 file_t *fp;
1088 struct gre_softc *sc = ifp->if_softc;
1089 struct proc *kp;
1090 struct socket *so;
1091 struct sockaddr_storage dst, src;
1092
1093 /* getsock() will FILE_USE() and unlock the descriptor for us */
1094 if ((fp = fd_getfile(fd)) == NULL) {
1095 GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
1096 return EINVAL;
1097 }
1098 if (fp->f_type != DTYPE_SOCKET) {
1099 fd_putfile(fd);
1100 return ENOTSOCK;
1101 }
1102
1103 /* Increase reference count. Now that our reference to
1104 * the file descriptor is counted, this thread can release
1105 * our "use" of the descriptor, but it will not be destroyed
1106 * by some other thread's action. This thread needs to
1107 * release its use, too, because one and only one thread
1108 * can have use of the descriptor at once. The kernel
1109 * thread will pick up the use if it needs it.
1110 */
1111 mutex_enter(&fp->f_lock);
1112 fp->f_count++;
1113 mutex_exit(&fp->f_lock);
1114 fd_putfile(fd);
1115 GRE_DPRINTF(sc, "%s: l.%d f_count %d\n", __func__, __LINE__,
1116 fp->f_count);
1117
1118 kp = sc->sc_lwp->l_proc;
1119 while ((error = fd_alloc(kp, 0, &kfd)) != 0 && error == ENOSPC)
1120 fd_tryexpand(kp);
1121 if (error != 0)
1122 goto closef;
1123 fd_affix(kp, fp, kfd);
1124
1125 GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
1126
1127 so = (struct socket *)fp->f_data;
1128 pr = so->so_proto;
1129 if ((pr->pr_flags & PR_ATOMIC) == 0 ||
1130 (sp->sp_type != 0 && pr->pr_type != sp->sp_type) ||
1131 (sp->sp_proto != 0 && pr->pr_protocol != 0 &&
1132 pr->pr_protocol != sp->sp_proto)) {
1133 GRE_DPRINTF(sc, "%s: l.%d, type %d, proto %d\n", __func__,
1134 __LINE__, pr->pr_type, pr->pr_protocol);
1135 error = EINVAL;
1136 goto release;
1137 }
1138
1139 GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
1140
1141 /* check address */
1142 if ((error = gre_getnames(so, curlwp, &src, &dst)) != 0)
1143 goto release;
1144
1145 GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
1146
1147 if (error != 0)
1148 goto release;
1149
1150 GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
1151
1152 sp->sp_src = src;
1153 sp->sp_dst = dst;
1154 /* fp does not any longer belong to this thread. */
1155 sp->sp_fd = kfd;
1156
1157 /* XXX print src & dst */
1158
1159 return 0;
1160 release:
1161 GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
1162 fd_abort(kp, fp, kfd);
1163 return error;
1164 closef:
1165 GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
1166 gre_closef(&fp);
1167 return error;
1168 }
1169
1170 static bool
1171 sockaddr_is_anyaddr(const struct sockaddr *sa)
1172 {
1173 socklen_t anylen, salen;
1174 const void *anyaddr, *addr;
1175
1176 if ((anyaddr = sockaddr_anyaddr(sa, &anylen)) == NULL ||
1177 (addr = sockaddr_const_addr(sa, &salen)) == NULL)
1178 return false;
1179
1180 if (salen > anylen)
1181 return false;
1182
1183 return memcmp(anyaddr, addr, MIN(anylen, salen)) == 0;
1184 }
1185
1186 static bool
1187 gre_is_nullconf(const struct gre_soparm *sp)
1188 {
1189 return sockaddr_is_anyaddr(sstocsa(&sp->sp_src)) ||
1190 sockaddr_is_anyaddr(sstocsa(&sp->sp_dst));
1191 }
1192
1193 static void
1194 gre_clearconf(struct gre_soparm *sp, bool force)
1195 {
1196 if (sp->sp_bysock || force) {
1197 sockaddr_copy(sstosa(&sp->sp_src), sizeof(sp->sp_src),
1198 sockaddr_any(sstosa(&sp->sp_src)));
1199 sockaddr_copy(sstosa(&sp->sp_dst), sizeof(sp->sp_dst),
1200 sockaddr_any(sstosa(&sp->sp_dst)));
1201 sp->sp_bysock = 0;
1202 }
1203 sp->sp_fd = -1;
1204 }
1205
1206 static int
1207 gre_ioctl_lock(struct gre_softc *sc)
1208 {
1209 mutex_enter(&sc->sc_mtx);
1210
1211 while (sc->sc_state == GRE_S_IOCTL)
1212 gre_wait(sc);
1213
1214 if (sc->sc_state != GRE_S_IDLE) {
1215 cv_signal(&sc->sc_condvar);
1216 mutex_exit(&sc->sc_mtx);
1217 GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
1218 return ENXIO;
1219 }
1220
1221 sc->sc_state = GRE_S_IOCTL;
1222
1223 mutex_exit(&sc->sc_mtx);
1224 return 0;
1225 }
1226
1227 static void
1228 gre_ioctl_unlock(struct gre_softc *sc)
1229 {
1230 mutex_enter(&sc->sc_mtx);
1231
1232 KASSERT(sc->sc_state == GRE_S_IOCTL);
1233 sc->sc_state = GRE_S_IDLE;
1234 cv_signal(&sc->sc_condvar);
1235
1236 mutex_exit(&sc->sc_mtx);
1237 }
1238
1239 static int
1240 gre_ioctl(struct ifnet *ifp, const u_long cmd, void *data)
1241 {
1242 struct ifreq *ifr;
1243 struct if_laddrreq *lifr = (struct if_laddrreq *)data;
1244 struct gre_softc *sc = ifp->if_softc;
1245 struct gre_soparm *sp;
1246 int fd, error = 0, oproto, otype, s;
1247 struct gre_soparm sp0;
1248
1249 ifr = data;
1250
1251 GRE_DPRINTF(sc, "%s: l.%d, cmd %lu\n", __func__, __LINE__, cmd);
1252
1253 switch (cmd) {
1254 case SIOCSIFFLAGS:
1255 case SIOCSIFMTU:
1256 case GRESPROTO:
1257 case GRESADDRD:
1258 case GRESADDRS:
1259 case GRESSOCK:
1260 case GREDSOCK:
1261 case SIOCSLIFPHYADDR:
1262 case SIOCDIFPHYADDR:
1263 if (kauth_authorize_network(curlwp->l_cred,
1264 KAUTH_NETWORK_INTERFACE,
1265 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd,
1266 NULL) != 0)
1267 return EPERM;
1268 break;
1269 default:
1270 break;
1271 }
1272
1273 if ((error = gre_ioctl_lock(sc)) != 0) {
1274 GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
1275 return error;
1276 }
1277 s = splnet();
1278
1279 sp0 = sc->sc_soparm;
1280 sp0.sp_fd = -1;
1281 sp = &sp0;
1282
1283 GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
1284
1285 switch (cmd) {
1286 case SIOCSIFADDR:
1287 GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
1288 if ((ifp->if_flags & IFF_UP) != 0)
1289 break;
1290 gre_clearconf(sp, false);
1291 ifp->if_flags |= IFF_UP;
1292 goto mksocket;
1293 case SIOCSIFDSTADDR:
1294 break;
1295 case SIOCSIFFLAGS:
1296 oproto = sp->sp_proto;
1297 otype = sp->sp_type;
1298 switch (ifr->ifr_flags & (IFF_LINK0|IFF_LINK2)) {
1299 case IFF_LINK0|IFF_LINK2:
1300 sp->sp_proto = IPPROTO_UDP;
1301 sp->sp_type = SOCK_DGRAM;
1302 break;
1303 case IFF_LINK2:
1304 sp->sp_proto = 0;
1305 sp->sp_type = 0;
1306 break;
1307 case IFF_LINK0:
1308 sp->sp_proto = IPPROTO_GRE;
1309 sp->sp_type = SOCK_RAW;
1310 break;
1311 default:
1312 GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
1313 error = EINVAL;
1314 goto out;
1315 }
1316 GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
1317 gre_clearconf(sp, false);
1318 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) ==
1319 (IFF_UP|IFF_RUNNING) &&
1320 (oproto == sp->sp_proto || sp->sp_proto == 0) &&
1321 (otype == sp->sp_type || sp->sp_type == 0))
1322 break;
1323 switch (sp->sp_proto) {
1324 case IPPROTO_UDP:
1325 case IPPROTO_GRE:
1326 goto mksocket;
1327 default:
1328 break;
1329 }
1330 break;
1331 case SIOCSIFMTU:
1332 /* XXX determine MTU automatically by probing w/
1333 * XXX do-not-fragment packets?
1334 */
1335 if (ifr->ifr_mtu < 576) {
1336 error = EINVAL;
1337 break;
1338 }
1339 /*FALLTHROUGH*/
1340 case SIOCGIFMTU:
1341 if ((error = ifioctl_common(ifp, cmd, data)) == ENETRESET)
1342 error = 0;
1343 break;
1344 case SIOCADDMULTI:
1345 case SIOCDELMULTI:
1346 if (ifr == NULL) {
1347 error = EAFNOSUPPORT;
1348 break;
1349 }
1350 switch (ifreq_getaddr(cmd, ifr)->sa_family) {
1351 #ifdef INET
1352 case AF_INET:
1353 break;
1354 #endif
1355 #ifdef INET6
1356 case AF_INET6:
1357 break;
1358 #endif
1359 default:
1360 error = EAFNOSUPPORT;
1361 break;
1362 }
1363 break;
1364 case GRESPROTO:
1365 gre_clearconf(sp, false);
1366 oproto = sp->sp_proto;
1367 otype = sp->sp_type;
1368 sp->sp_proto = ifr->ifr_flags;
1369 switch (sp->sp_proto) {
1370 case IPPROTO_UDP:
1371 ifp->if_flags |= IFF_LINK0|IFF_LINK2;
1372 sp->sp_type = SOCK_DGRAM;
1373 break;
1374 case IPPROTO_GRE:
1375 ifp->if_flags |= IFF_LINK0;
1376 ifp->if_flags &= ~IFF_LINK2;
1377 sp->sp_type = SOCK_RAW;
1378 break;
1379 case 0:
1380 ifp->if_flags &= ~IFF_LINK0;
1381 ifp->if_flags |= IFF_LINK2;
1382 sp->sp_type = 0;
1383 break;
1384 default:
1385 error = EPROTONOSUPPORT;
1386 break;
1387 }
1388 if ((oproto == sp->sp_proto || sp->sp_proto == 0) &&
1389 (otype == sp->sp_type || sp->sp_type == 0))
1390 break;
1391 switch (sp->sp_proto) {
1392 case IPPROTO_UDP:
1393 case IPPROTO_GRE:
1394 goto mksocket;
1395 default:
1396 break;
1397 }
1398 break;
1399 case GREGPROTO:
1400 ifr->ifr_flags = sp->sp_proto;
1401 break;
1402 case GRESADDRS:
1403 case GRESADDRD:
1404 gre_clearconf(sp, false);
1405 /*
1406 * set tunnel endpoints, compute a less specific route
1407 * to the remote end and mark if as up
1408 */
1409 switch (cmd) {
1410 case GRESADDRS:
1411 sockaddr_copy(sstosa(&sp->sp_src),
1412 sizeof(sp->sp_src), ifreq_getaddr(cmd, ifr));
1413 break;
1414 case GRESADDRD:
1415 sockaddr_copy(sstosa(&sp->sp_dst),
1416 sizeof(sp->sp_dst), ifreq_getaddr(cmd, ifr));
1417 break;
1418 }
1419 checkaddr:
1420 if (sockaddr_any(sstosa(&sp->sp_src)) == NULL ||
1421 sockaddr_any(sstosa(&sp->sp_dst)) == NULL) {
1422 error = EINVAL;
1423 break;
1424 }
1425 /* let gre_socreate() check the rest */
1426 mksocket:
1427 GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
1428 /* If we're administratively down, or the configuration
1429 * is empty, there's no use creating a socket.
1430 */
1431 if ((ifp->if_flags & IFF_UP) == 0 || gre_is_nullconf(sp))
1432 goto sendconf;
1433
1434 GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
1435 fd = 0;
1436 error = gre_socreate(sc, sp, &fd);
1437 if (error != 0)
1438 break;
1439
1440 setsock:
1441 GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
1442
1443 error = gre_ssock(ifp, sp, fd);
1444
1445 if (cmd != GRESSOCK) {
1446 GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
1447 /* XXX v. dodgy */
1448 if (fd_getfile(fd) != NULL)
1449 fd_close(fd);
1450 }
1451
1452 if (error == 0) {
1453 sendconf:
1454 GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
1455 ifp->if_flags &= ~IFF_RUNNING;
1456 sc->sc_so = gre_reconf(sc, sc->sc_so, sc->sc_lwp, sp);
1457 }
1458
1459 break;
1460 case GREGADDRS:
1461 ifreq_setaddr(cmd, ifr, sstosa(&sp->sp_src));
1462 break;
1463 case GREGADDRD:
1464 ifreq_setaddr(cmd, ifr, sstosa(&sp->sp_dst));
1465 break;
1466 case GREDSOCK:
1467 GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
1468 if (sp->sp_bysock)
1469 ifp->if_flags &= ~IFF_UP;
1470 gre_clearconf(sp, false);
1471 goto mksocket;
1472 case GRESSOCK:
1473 GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
1474 gre_clearconf(sp, true);
1475 fd = (int)ifr->ifr_value;
1476 sp->sp_bysock = 1;
1477 ifp->if_flags |= IFF_UP;
1478 goto setsock;
1479 case SIOCSLIFPHYADDR:
1480 GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
1481 if (lifr->addr.ss_family != lifr->dstaddr.ss_family) {
1482 error = EAFNOSUPPORT;
1483 break;
1484 }
1485 sockaddr_copy(sstosa(&sp->sp_src), sizeof(sp->sp_src),
1486 sstosa(&lifr->addr));
1487 sockaddr_copy(sstosa(&sp->sp_dst), sizeof(sp->sp_dst),
1488 sstosa(&lifr->dstaddr));
1489 GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
1490 goto checkaddr;
1491 case SIOCDIFPHYADDR:
1492 GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
1493 gre_clearconf(sp, true);
1494 ifp->if_flags &= ~IFF_UP;
1495 goto mksocket;
1496 case SIOCGLIFPHYADDR:
1497 GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
1498 if (gre_is_nullconf(sp)) {
1499 error = EADDRNOTAVAIL;
1500 break;
1501 }
1502 sockaddr_copy(sstosa(&lifr->addr), sizeof(lifr->addr),
1503 sstosa(&sp->sp_src));
1504 sockaddr_copy(sstosa(&lifr->dstaddr), sizeof(lifr->dstaddr),
1505 sstosa(&sp->sp_dst));
1506 GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
1507 break;
1508 default:
1509 error = EINVAL;
1510 break;
1511 }
1512 out:
1513 GRE_DPRINTF(sc, "%s: l.%d\n", __func__, __LINE__);
1514 splx(s);
1515 gre_ioctl_unlock(sc);
1516 return error;
1517 }
1518
1519 #endif
1520
1521 void greattach(int);
1522
1523 /* ARGSUSED */
1524 void
1525 greattach(int count)
1526 {
1527 #ifdef INET
1528 if_clone_attach(&gre_cloner);
1529 #endif
1530 }
1531