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