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