if_gre.c revision 1.75 1 /* $NetBSD: if_gre.c,v 1.75 2006/11/16 22:26:35 dyoung Exp $ */
2
3 /*
4 * Copyright (c) 1998 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 * Also supported: IP in IP encaps (proto 55) as of RFC 2004
48 */
49
50 #include <sys/cdefs.h>
51 __KERNEL_RCSID(0, "$NetBSD: if_gre.c,v 1.75 2006/11/16 22:26:35 dyoung Exp $");
52
53 #include "opt_gre.h"
54 #include "opt_inet.h"
55 #include "bpfilter.h"
56
57 #ifdef INET
58 #include <sys/param.h>
59 #include <sys/file.h>
60 #include <sys/filedesc.h>
61 #include <sys/malloc.h>
62 #include <sys/mbuf.h>
63 #include <sys/proc.h>
64 #include <sys/protosw.h>
65 #include <sys/socket.h>
66 #include <sys/socketvar.h>
67 #include <sys/ioctl.h>
68 #include <sys/queue.h>
69 #if __NetBSD__
70 #include <sys/systm.h>
71 #include <sys/sysctl.h>
72 #include <sys/kauth.h>
73 #endif
74
75 #include <sys/kthread.h>
76
77 #include <machine/cpu.h>
78
79 #include <net/ethertypes.h>
80 #include <net/if.h>
81 #include <net/if_types.h>
82 #include <net/netisr.h>
83 #include <net/route.h>
84
85 #ifdef INET
86 #include <netinet/in.h>
87 #include <netinet/in_systm.h>
88 #include <netinet/in_var.h>
89 #include <netinet/ip.h>
90 #include <netinet/ip_var.h>
91 #else
92 #error "Huh? if_gre without inet?"
93 #endif
94
95
96 #ifdef NETATALK
97 #include <netatalk/at.h>
98 #include <netatalk/at_var.h>
99 #include <netatalk/at_extern.h>
100 #endif
101
102 #if NBPFILTER > 0
103 #include <sys/time.h>
104 #include <net/bpf.h>
105 #endif
106
107 #include <net/if_gre.h>
108
109 /*
110 * It is not easy to calculate the right value for a GRE MTU.
111 * We leave this task to the admin and use the same default that
112 * other vendors use.
113 */
114 #define GREMTU 1476
115
116 #ifdef GRE_DEBUG
117 #define GRE_DPRINTF(__sc, __fmt, ...) \
118 do { \
119 if (((__sc)->sc_if.if_flags & IFF_DEBUG) != 0) \
120 printf(__fmt, __VA_ARGS__); \
121 } while (/*CONSTCOND*/0)
122 #else
123 #define GRE_DPRINTF(__sc, __fmt, ...) do { } while (/*CONSTCOND*/0)
124 #endif /* GRE_DEBUG */
125
126 struct gre_softc_head gre_softc_list;
127 int ip_gre_ttl = GRE_TTL;
128
129 static int gre_clone_create(struct if_clone *, int);
130 static int gre_clone_destroy(struct ifnet *);
131
132 static struct if_clone gre_cloner =
133 IF_CLONE_INITIALIZER("gre", gre_clone_create, gre_clone_destroy);
134
135 static int gre_output(struct ifnet *, struct mbuf *, struct sockaddr *,
136 struct rtentry *);
137 static int gre_ioctl(struct ifnet *, u_long, caddr_t);
138
139 static int gre_compute_route(struct gre_softc *sc);
140
141 static int gre_getsockname(struct socket *, struct mbuf *, struct lwp *);
142 static int gre_getpeername(struct socket *, struct mbuf *, struct lwp *);
143 static int gre_getnames(struct socket *, struct lwp *, struct sockaddr_in *,
144 struct sockaddr_in *);
145
146 static void
147 gre_stop(int *running)
148 {
149 *running = 0;
150 wakeup(running);
151 }
152
153 static void
154 gre_join(int *running)
155 {
156 int s;
157
158 s = splnet();
159 while (*running != 0) {
160 splx(s);
161 tsleep(running, PSOCK, "grejoin", 0);
162 s = splnet();
163 }
164 splx(s);
165 }
166
167 static void
168 gre_wakeup(struct gre_softc *sc)
169 {
170 GRE_DPRINTF(sc, "%s: enter\n", __func__);
171 sc->sc_waitchan = 1;
172 wakeup(&sc->sc_waitchan);
173 }
174
175 static int
176 gre_clone_create(struct if_clone *ifc, int unit)
177 {
178 struct gre_softc *sc;
179
180 sc = malloc(sizeof(struct gre_softc), M_DEVBUF, M_WAITOK);
181 memset(sc, 0, sizeof(struct gre_softc));
182
183 snprintf(sc->sc_if.if_xname, sizeof(sc->sc_if.if_xname), "%s%d",
184 ifc->ifc_name, unit);
185 sc->sc_if.if_softc = sc;
186 sc->sc_if.if_type = IFT_TUNNEL;
187 sc->sc_if.if_addrlen = 0;
188 sc->sc_if.if_hdrlen = 24; /* IP + GRE */
189 sc->sc_if.if_dlt = DLT_NULL;
190 sc->sc_if.if_mtu = GREMTU;
191 sc->sc_if.if_flags = IFF_POINTOPOINT|IFF_MULTICAST;
192 sc->sc_if.if_output = gre_output;
193 sc->sc_if.if_ioctl = gre_ioctl;
194 sc->g_dst.s_addr = sc->g_src.s_addr = INADDR_ANY;
195 sc->g_dstport = sc->g_srcport = 0;
196 sc->sc_proto = IPPROTO_GRE;
197 sc->sc_snd.ifq_maxlen = 256;
198 sc->sc_if.if_flags |= IFF_LINK0;
199 if_attach(&sc->sc_if);
200 if_alloc_sadl(&sc->sc_if);
201 #if NBPFILTER > 0
202 bpfattach(&sc->sc_if, DLT_NULL, sizeof(u_int32_t));
203 #endif
204 LIST_INSERT_HEAD(&gre_softc_list, sc, sc_list);
205 return (0);
206 }
207
208 static int
209 gre_clone_destroy(struct ifnet *ifp)
210 {
211 struct gre_softc *sc = ifp->if_softc;
212
213 LIST_REMOVE(sc, sc_list);
214 #if NBPFILTER > 0
215 bpfdetach(ifp);
216 #endif
217 if_detach(ifp);
218 gre_wakeup(sc);
219 gre_join(&sc->sc_thread);
220 if (sc->sc_fp != NULL) {
221 closef(sc->sc_fp, curlwp);
222 sc->sc_fp = NULL;
223 }
224 free(sc, M_DEVBUF);
225
226 return (0);
227 }
228
229 static void
230 gre_receive(struct socket *so, caddr_t arg, int waitflag)
231 {
232 struct gre_softc *sc = (struct gre_softc *)arg;
233
234 GRE_DPRINTF(sc, "%s: enter\n", __func__);
235
236 gre_wakeup(sc);
237 }
238
239 static void
240 gre_upcall_add(struct socket *so, caddr_t arg)
241 {
242 /* XXX What if the kernel already set an upcall? */
243 so->so_upcallarg = arg;
244 so->so_upcall = gre_receive;
245 so->so_rcv.sb_flags |= SB_UPCALL;
246 }
247
248 static void
249 gre_upcall_remove(struct socket *so)
250 {
251 /* XXX What if the kernel already set an upcall? */
252 so->so_rcv.sb_flags &= ~SB_UPCALL;
253 so->so_upcallarg = NULL;
254 so->so_upcall = NULL;
255 }
256
257 static void
258 gre_sodestroy(struct socket **sop)
259 {
260 gre_upcall_remove(*sop);
261 soshutdown(*sop, SHUT_RDWR);
262 soclose(*sop);
263 *sop = NULL;
264 }
265
266 static struct mbuf *
267 gre_getsockmbuf(struct socket *so)
268 {
269 struct mbuf *m;
270
271 m = m_get(M_WAIT, MT_SONAME);
272 if (m != NULL)
273 MCLAIM(m, so->so_mowner);
274 return m;
275 }
276
277 static int
278 gre_socreate1(struct gre_softc *sc, struct lwp *l, struct gre_soparm *sp,
279 struct socket **sop)
280 {
281 int rc;
282 struct mbuf *m;
283 struct sockaddr_in *sin;
284 struct socket *so;
285
286 GRE_DPRINTF(sc, "%s: enter\n", __func__);
287 rc = socreate(AF_INET, sop, SOCK_DGRAM, IPPROTO_UDP, l);
288 if (rc != 0) {
289 GRE_DPRINTF(sc, "%s: socreate failed\n", __func__);
290 return rc;
291 }
292
293 so = *sop;
294
295 gre_upcall_add(so, (caddr_t)sc);
296 if ((m = gre_getsockmbuf(so)) == NULL) {
297 rc = ENOBUFS;
298 goto out;
299 }
300 sin = mtod(m, struct sockaddr_in *);
301 sin->sin_len = m->m_len = sizeof(struct sockaddr_in);
302 sin->sin_family = AF_INET;
303 sin->sin_addr = sc->g_src;
304 sin->sin_port = sc->g_srcport;
305
306 GRE_DPRINTF(sc, "%s: bind 0x%08" PRIx32 " port %d\n", __func__,
307 sin->sin_addr.s_addr, ntohs(sin->sin_port));
308 if ((rc = sobind(so, m, l)) != 0) {
309 GRE_DPRINTF(sc, "%s: sobind failed\n", __func__);
310 goto out;
311 }
312
313 if (sc->g_srcport == 0) {
314 if ((rc = gre_getsockname(so, m, l)) != 0) {
315 GRE_DPRINTF(sc, "%s: gre_getsockname failed\n",
316 __func__);
317 goto out;
318 }
319 sc->g_srcport = sin->sin_port;
320 }
321
322 sin->sin_addr = sc->g_dst;
323 sin->sin_port = sc->g_dstport;
324
325 if ((rc = soconnect(so, m, l)) != 0) {
326 GRE_DPRINTF(sc, "%s: soconnect failed\n", __func__);
327 goto out;
328 }
329
330 *mtod(m, int *) = ip_gre_ttl;
331 rc = (*so->so_proto->pr_ctloutput)(PRCO_SETOPT, so, IPPROTO_IP, IP_TTL,
332 &m);
333 m = NULL;
334 if (rc != 0) {
335 printf("%s: setopt ttl failed\n", __func__);
336 rc = 0;
337 }
338 out:
339 m_freem(m);
340
341 if (rc != 0)
342 gre_sodestroy(sop);
343 else
344 *sp = sc->sc_soparm;
345
346 return rc;
347 }
348
349 static void
350 gre_thread1(struct gre_softc *sc, struct lwp *l)
351 {
352 int flags, rc, s;
353 const struct gre_h *gh;
354 struct ifnet *ifp = &sc->sc_if;
355 struct mbuf *m;
356 struct socket *so = NULL;
357 struct uio uio;
358 struct gre_soparm sp;
359
360 GRE_DPRINTF(sc, "%s: enter\n", __func__);
361 s = splnet();
362
363 sc->sc_waitchan = 1;
364
365 memset(&sp, 0, sizeof(sp));
366 memset(&uio, 0, sizeof(uio));
367
368 ifp->if_flags |= IFF_RUNNING;
369
370 for (;;) {
371 while (sc->sc_waitchan == 0) {
372 splx(s);
373 GRE_DPRINTF(sc, "%s: sleeping\n", __func__);
374 tsleep(&sc->sc_waitchan, PSOCK, "grewait", 0);
375 s = splnet();
376 }
377 sc->sc_waitchan = 0;
378 GRE_DPRINTF(sc, "%s: awake\n", __func__);
379 if ((ifp->if_flags & IFF_UP) != IFF_UP) {
380 GRE_DPRINTF(sc, "%s: not up & running; exiting\n",
381 __func__);
382 break;
383 }
384 if (sc->sc_proto != IPPROTO_UDP) {
385 GRE_DPRINTF(sc, "%s: not udp; exiting\n", __func__);
386 break;
387 }
388 /* XXX optimize */
389 if (so == NULL || memcmp(&sp, &sc->sc_soparm, sizeof(sp)) != 0){
390 GRE_DPRINTF(sc, "%s: parameters changed\n", __func__);
391
392 if (sp.sp_fp != NULL) {
393 FILE_UNUSE(sp.sp_fp, NULL);
394 sp.sp_fp = NULL;
395 so = NULL;
396 } else if (so != NULL)
397 gre_sodestroy(&so);
398
399 if (sc->sc_fp != NULL) {
400 so = (struct socket *)sc->sc_fp->f_data;
401 gre_upcall_add(so, (caddr_t)sc);
402 sp = sc->sc_soparm;
403 FILE_USE(sp.sp_fp);
404 } else if (gre_socreate1(sc, l, &sp, &so) != 0)
405 goto out;
406 }
407 for (;;) {
408 flags = MSG_DONTWAIT;
409 uio.uio_resid = 1000000;
410 rc = (*so->so_receive)(so, NULL, &uio, &m, NULL,
411 &flags);
412 /* TBD Back off if ECONNREFUSED (indicates
413 * ICMP Port Unreachable)?
414 */
415 if (rc == EWOULDBLOCK) {
416 GRE_DPRINTF(sc, "%s: so_receive EWOULDBLOCK\n",
417 __func__);
418 break;
419 } else if (rc != 0 || m == NULL) {
420 GRE_DPRINTF(sc, "%s: rc %d m %p\n",
421 ifp->if_xname, rc, (void *)m);
422 continue;
423 } else
424 GRE_DPRINTF(sc, "%s: so_receive ok\n",
425 __func__);
426 if (m->m_len < sizeof(*gh) &&
427 (m = m_pullup(m, sizeof(*gh))) == NULL) {
428 GRE_DPRINTF(sc, "%s: m_pullup failed\n",
429 __func__);
430 continue;
431 }
432 gh = mtod(m, const struct gre_h *);
433
434 if (gre_input3(sc, m, 0, IPPROTO_GRE, gh) == 0) {
435 GRE_DPRINTF(sc, "%s: dropping unsupported\n",
436 __func__);
437 ifp->if_ierrors++;
438 m_freem(m);
439 }
440 }
441 for (;;) {
442 IF_DEQUEUE(&sc->sc_snd, m);
443 if (m == NULL)
444 break;
445 GRE_DPRINTF(sc, "%s: dequeue\n", __func__);
446 if ((so->so_state & SS_ISCONNECTED) == 0) {
447 GRE_DPRINTF(sc, "%s: not connected\n",
448 __func__);
449 m_freem(m);
450 continue;
451 }
452 rc = (*so->so_send)(so, NULL, NULL, m, NULL, 0, l);
453 /* XXX handle ENOBUFS? */
454 if (rc != 0)
455 GRE_DPRINTF(sc, "%s: so_send failed\n",
456 __func__);
457 }
458 /* Give the software interrupt queues a chance to
459 * run, or else when I send a ping from gre0 to gre1 on
460 * the same host, gre0 will not wake for the reply.
461 */
462 splx(s);
463 s = splnet();
464 }
465 if (sp.sp_fp != NULL) {
466 GRE_DPRINTF(sc, "%s: removing upcall\n", __func__);
467 gre_upcall_remove(so);
468 FILE_UNUSE(sp.sp_fp, NULL);
469 sp.sp_fp = NULL;
470 } else if (so != NULL)
471 gre_sodestroy(&so);
472 out:
473 GRE_DPRINTF(sc, "%s: stopping\n", __func__);
474 if (sc->sc_proto == IPPROTO_UDP)
475 ifp->if_flags &= ~IFF_RUNNING;
476 while (!IF_IS_EMPTY(&sc->sc_snd)) {
477 IF_DEQUEUE(&sc->sc_snd, m);
478 m_freem(m);
479 }
480 gre_stop(&sc->sc_thread);
481 /* must not touch sc after this! */
482 GRE_DPRINTF(sc, "%s: restore ipl\n", __func__);
483 splx(s);
484 }
485
486 static void
487 gre_thread(void *arg)
488 {
489 struct gre_softc *sc = (struct gre_softc *)arg;
490
491 gre_thread1(sc, curlwp);
492 /* must not touch sc after this! */
493 kthread_exit(0);
494 }
495
496 int
497 gre_input3(struct gre_softc *sc, struct mbuf *m, int hlen, u_char proto,
498 const struct gre_h *gh)
499 {
500 u_int16_t flags;
501 #if NBPFILTER > 0
502 u_int32_t af = AF_INET; /* af passed to BPF tap */
503 #endif
504 int s, isr;
505 struct ifqueue *ifq;
506
507 sc->sc_if.if_ipackets++;
508 sc->sc_if.if_ibytes += m->m_pkthdr.len;
509
510 switch (proto) {
511 case IPPROTO_GRE:
512 hlen += sizeof(struct gre_h);
513
514 /* process GRE flags as packet can be of variable len */
515 flags = ntohs(gh->flags);
516
517 /* Checksum & Offset are present */
518 if ((flags & GRE_CP) | (flags & GRE_RP))
519 hlen += 4;
520 /* We don't support routing fields (variable length) */
521 if (flags & GRE_RP)
522 return (0);
523 if (flags & GRE_KP)
524 hlen += 4;
525 if (flags & GRE_SP)
526 hlen += 4;
527
528 switch (ntohs(gh->ptype)) { /* ethertypes */
529 case ETHERTYPE_IP: /* shouldn't need a schednetisr(), as */
530 ifq = &ipintrq; /* we are in ip_input */
531 isr = NETISR_IP;
532 break;
533 #ifdef NETATALK
534 case ETHERTYPE_ATALK:
535 ifq = &atintrq1;
536 isr = NETISR_ATALK;
537 #if NBPFILTER > 0
538 af = AF_APPLETALK;
539 #endif
540 break;
541 #endif
542 #ifdef INET6
543 case ETHERTYPE_IPV6:
544 GRE_DPRINTF(sc, "%s: IPv6 packet\n", __func__);
545 ifq = &ip6intrq;
546 isr = NETISR_IPV6;
547 #if NBPFILTER > 0
548 af = AF_INET6;
549 #endif
550 break;
551 #endif
552 default: /* others not yet supported */
553 printf("%s: unhandled ethertype 0x%04x\n", __func__,
554 ntohs(gh->ptype));
555 return (0);
556 }
557 break;
558 default:
559 /* others not yet supported */
560 return (0);
561 }
562
563 if (hlen > m->m_pkthdr.len) {
564 m_freem(m);
565 sc->sc_if.if_ierrors++;
566 return (EINVAL);
567 }
568 m_adj(m, hlen);
569
570 #if NBPFILTER > 0
571 if (sc->sc_if.if_bpf != NULL)
572 bpf_mtap_af(sc->sc_if.if_bpf, af, m);
573 #endif /*NBPFILTER > 0*/
574
575 m->m_pkthdr.rcvif = &sc->sc_if;
576
577 s = splnet(); /* possible */
578 if (IF_QFULL(ifq)) {
579 IF_DROP(ifq);
580 m_freem(m);
581 } else {
582 IF_ENQUEUE(ifq, m);
583 }
584 /* we need schednetisr since the address family may change */
585 schednetisr(isr);
586 splx(s);
587
588 return (1); /* packet is done, no further processing needed */
589 }
590
591 /*
592 * The output routine. Takes a packet and encapsulates it in the protocol
593 * given by sc->sc_proto. See also RFC 1701 and RFC 2004
594 */
595 static int
596 gre_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
597 struct rtentry *rt)
598 {
599 int error = 0, hlen;
600 struct gre_softc *sc = ifp->if_softc;
601 struct greip *gi;
602 struct gre_h *gh;
603 struct ip *eip, *ip;
604 u_int8_t ip_tos = 0;
605 u_int16_t etype = 0;
606 struct mobile_h mob_h;
607
608 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == 0 ||
609 sc->g_src.s_addr == INADDR_ANY || sc->g_dst.s_addr == INADDR_ANY) {
610 m_freem(m);
611 error = ENETDOWN;
612 goto end;
613 }
614
615 gi = NULL;
616 ip = NULL;
617
618 #if NBPFILTER >0
619 if (ifp->if_bpf)
620 bpf_mtap_af(ifp->if_bpf, dst->sa_family, m);
621 #endif
622
623 m->m_flags &= ~(M_BCAST|M_MCAST);
624
625 switch (sc->sc_proto) {
626 case IPPROTO_MOBILE:
627 if (dst->sa_family == AF_INET) {
628 int msiz;
629
630 if (M_UNWRITABLE(m, sizeof(*ip)) &&
631 (m = m_pullup(m, sizeof(*ip))) == NULL) {
632 error = ENOBUFS;
633 goto end;
634 }
635 ip = mtod(m, struct ip *);
636
637 memset(&mob_h, 0, MOB_H_SIZ_L);
638 mob_h.proto = (ip->ip_p) << 8;
639 mob_h.odst = ip->ip_dst.s_addr;
640 ip->ip_dst.s_addr = sc->g_dst.s_addr;
641
642 /*
643 * If the packet comes from our host, we only change
644 * the destination address in the IP header.
645 * Else we also need to save and change the source
646 */
647 if (in_hosteq(ip->ip_src, sc->g_src)) {
648 msiz = MOB_H_SIZ_S;
649 } else {
650 mob_h.proto |= MOB_H_SBIT;
651 mob_h.osrc = ip->ip_src.s_addr;
652 ip->ip_src.s_addr = sc->g_src.s_addr;
653 msiz = MOB_H_SIZ_L;
654 }
655 HTONS(mob_h.proto);
656 mob_h.hcrc = gre_in_cksum((u_int16_t *)&mob_h, msiz);
657
658 M_PREPEND(m, msiz, M_DONTWAIT);
659 if (m == NULL) {
660 error = ENOBUFS;
661 goto end;
662 }
663 /* XXX Assuming that ip does not dangle after
664 * M_PREPEND. In practice, that's true, but
665 * that's in M_PREPEND's contract.
666 */
667 memmove(mtod(m, caddr_t), ip, sizeof(*ip));
668 ip = mtod(m, struct ip *);
669 memcpy((caddr_t)(ip + 1), &mob_h, (unsigned)msiz);
670 ip->ip_len = htons(ntohs(ip->ip_len) + msiz);
671 } else { /* AF_INET */
672 IF_DROP(&ifp->if_snd);
673 m_freem(m);
674 error = EINVAL;
675 goto end;
676 }
677 break;
678 case IPPROTO_UDP:
679 case IPPROTO_GRE:
680 GRE_DPRINTF(sc, "%s: dst->sa_family=%d\n", __func__,
681 dst->sa_family);
682 switch (dst->sa_family) {
683 case AF_INET:
684 ip = mtod(m, struct ip *);
685 ip_tos = ip->ip_tos;
686 etype = ETHERTYPE_IP;
687 break;
688 #ifdef NETATALK
689 case AF_APPLETALK:
690 etype = ETHERTYPE_ATALK;
691 break;
692 #endif
693 #ifdef INET6
694 case AF_INET6:
695 etype = ETHERTYPE_IPV6;
696 break;
697 #endif
698 default:
699 IF_DROP(&ifp->if_snd);
700 m_freem(m);
701 error = EAFNOSUPPORT;
702 goto end;
703 }
704 break;
705 default:
706 IF_DROP(&ifp->if_snd);
707 m_freem(m);
708 error = EINVAL;
709 goto end;
710 }
711
712 switch (sc->sc_proto) {
713 case IPPROTO_GRE:
714 hlen = sizeof(struct greip);
715 break;
716 case IPPROTO_UDP:
717 hlen = sizeof(struct gre_h);
718 break;
719 default:
720 hlen = 0;
721 break;
722 }
723
724 M_PREPEND(m, hlen, M_DONTWAIT);
725
726 if (m == NULL) {
727 IF_DROP(&ifp->if_snd);
728 error = ENOBUFS;
729 goto end;
730 }
731
732 switch (sc->sc_proto) {
733 case IPPROTO_UDP:
734 gh = mtod(m, struct gre_h *);
735 memset(gh, 0, sizeof(*gh));
736 gh->ptype = htons(etype);
737 /* XXX Need to handle IP ToS. Look at how I handle IP TTL. */
738 break;
739 case IPPROTO_GRE:
740 gi = mtod(m, struct greip *);
741 gh = &gi->gi_g;
742 eip = &gi->gi_i;
743 /* we don't have any GRE flags for now */
744 memset(gh, 0, sizeof(*gh));
745 gh->ptype = htons(etype);
746 eip->ip_src = sc->g_src;
747 eip->ip_dst = sc->g_dst;
748 eip->ip_hl = (sizeof(struct ip)) >> 2;
749 eip->ip_ttl = ip_gre_ttl;
750 eip->ip_tos = ip_tos;
751 eip->ip_len = htons(m->m_pkthdr.len);
752 eip->ip_p = sc->sc_proto;
753 break;
754 case IPPROTO_MOBILE:
755 eip = mtod(m, struct ip *);
756 eip->ip_p = sc->sc_proto;
757 break;
758 default:
759 error = EPROTONOSUPPORT;
760 m_freem(m);
761 goto end;
762 }
763
764 ifp->if_opackets++;
765 ifp->if_obytes += m->m_pkthdr.len;
766
767 /* send it off */
768 if (sc->sc_proto == IPPROTO_UDP) {
769 if (IF_QFULL(&sc->sc_snd)) {
770 IF_DROP(&sc->sc_snd);
771 error = ENOBUFS;
772 m_freem(m);
773 } else {
774 IF_ENQUEUE(&sc->sc_snd, m);
775 gre_wakeup(sc);
776 error = 0;
777 }
778 } else {
779 error = ip_output(m, NULL, &sc->route, 0,
780 (struct ip_moptions *)NULL, (struct socket *)NULL);
781 }
782 end:
783 if (error)
784 ifp->if_oerrors++;
785 return (error);
786 }
787
788 /* gre_kick must be synchronized with network interrupts in order
789 * to synchronize access to gre_softc members, so call it with
790 * interrupt priority level set to IPL_NET or greater.
791 */
792 static int
793 gre_kick(struct gre_softc *sc)
794 {
795 int rc;
796 struct ifnet *ifp = &sc->sc_if;
797
798 if (sc->sc_proto == IPPROTO_UDP && (ifp->if_flags & IFF_UP) == IFF_UP &&
799 !sc->sc_thread) {
800 sc->sc_thread = 1;
801 rc = kthread_create1(gre_thread, (void *)sc, NULL,
802 ifp->if_xname);
803 if (rc != 0)
804 gre_stop(&sc->sc_thread);
805 return rc;
806 } else {
807 gre_wakeup(sc);
808 return 0;
809 }
810 }
811
812 static int
813 gre_getname(struct socket *so, int req, struct mbuf *nam, struct lwp *l)
814 {
815 int s, error;
816
817 s = splsoftnet();
818 error = (*so->so_proto->pr_usrreq)(so, req, (struct mbuf *)0,
819 nam, (struct mbuf *)0, l);
820 splx(s);
821 return error;
822 }
823
824 static int
825 gre_getsockname(struct socket *so, struct mbuf *nam, struct lwp *l)
826 {
827 return gre_getname(so, PRU_SOCKADDR, nam, l);
828 }
829
830 static int
831 gre_getpeername(struct socket *so, struct mbuf *nam, struct lwp *l)
832 {
833 return gre_getname(so, PRU_PEERADDR, nam, l);
834 }
835
836 static int
837 gre_getnames(struct socket *so, struct lwp *l, struct sockaddr_in *src,
838 struct sockaddr_in *dst)
839 {
840 struct mbuf *m;
841 struct sockaddr_in *sin;
842 int rc;
843
844 if ((m = gre_getsockmbuf(so)) == NULL)
845 return ENOBUFS;
846
847 sin = mtod(m, struct sockaddr_in *);
848
849 if ((rc = gre_getsockname(so, m, l)) != 0)
850 goto out;
851 if (sin->sin_family != AF_INET) {
852 rc = EAFNOSUPPORT;
853 goto out;
854 }
855 *src = *sin;
856
857 if ((rc = gre_getpeername(so, m, l)) != 0)
858 goto out;
859 if (sin->sin_family != AF_INET) {
860 rc = EAFNOSUPPORT;
861 goto out;
862 }
863 *dst = *sin;
864
865 out:
866 m_freem(m);
867 return rc;
868 }
869
870 static int
871 gre_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
872 {
873 u_char oproto;
874 struct file *fp, *ofp;
875 struct socket *so;
876 struct sockaddr_in dst, src;
877 struct proc *p = curproc; /* XXX */
878 struct lwp *l = curlwp; /* XXX */
879 struct ifreq *ifr = (struct ifreq *)data;
880 struct if_laddrreq *lifr = (struct if_laddrreq *)data;
881 struct gre_softc *sc = ifp->if_softc;
882 int s;
883 struct sockaddr_in si;
884 struct sockaddr *sa = NULL;
885 int error;
886
887 switch (cmd) {
888 case SIOCSIFFLAGS:
889 case SIOCSIFMTU:
890 case GRESPROTO:
891 case GRESADDRD:
892 case GRESADDRS:
893 case GRESSOCK:
894 case GREDSOCK:
895 case SIOCSLIFPHYADDR:
896 case SIOCDIFPHYADDR:
897 if (kauth_authorize_network(l->l_cred, KAUTH_NETWORK_INTERFACE,
898 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd,
899 NULL) != 0)
900 return (EPERM);
901 break;
902 default:
903 error = 0;
904 break;
905 }
906
907 s = splnet();
908 switch (cmd) {
909 case SIOCSIFADDR:
910 ifp->if_flags |= IFF_UP;
911 error = gre_kick(sc);
912 break;
913 case SIOCSIFDSTADDR:
914 break;
915 case SIOCSIFFLAGS:
916 oproto = sc->sc_proto;
917 switch (ifr->ifr_flags & (IFF_LINK0|IFF_LINK2)) {
918 case IFF_LINK0|IFF_LINK2:
919 sc->sc_proto = IPPROTO_UDP;
920 if (oproto != IPPROTO_UDP)
921 ifp->if_flags &= ~IFF_RUNNING;
922 error = gre_kick(sc);
923 break;
924 case IFF_LINK0:
925 sc->sc_proto = IPPROTO_GRE;
926 gre_wakeup(sc);
927 goto recompute;
928 case 0:
929 sc->sc_proto = IPPROTO_MOBILE;
930 gre_wakeup(sc);
931 goto recompute;
932 }
933 break;
934 case SIOCSIFMTU:
935 if (ifr->ifr_mtu < 576) {
936 error = EINVAL;
937 break;
938 }
939 ifp->if_mtu = ifr->ifr_mtu;
940 break;
941 case SIOCGIFMTU:
942 ifr->ifr_mtu = sc->sc_if.if_mtu;
943 break;
944 case SIOCADDMULTI:
945 case SIOCDELMULTI:
946 if (ifr == 0) {
947 error = EAFNOSUPPORT;
948 break;
949 }
950 switch (ifr->ifr_addr.sa_family) {
951 #ifdef INET
952 case AF_INET:
953 break;
954 #endif
955 #ifdef INET6
956 case AF_INET6:
957 break;
958 #endif
959 default:
960 error = EAFNOSUPPORT;
961 break;
962 }
963 break;
964 case GRESPROTO:
965 oproto = sc->sc_proto;
966 sc->sc_proto = ifr->ifr_flags;
967 switch (sc->sc_proto) {
968 case IPPROTO_UDP:
969 ifp->if_flags |= IFF_LINK0|IFF_LINK2;
970 if (oproto != IPPROTO_UDP)
971 ifp->if_flags &= ~IFF_RUNNING;
972 error = gre_kick(sc);
973 break;
974 case IPPROTO_GRE:
975 ifp->if_flags |= IFF_LINK0;
976 ifp->if_flags &= ~IFF_LINK2;
977 goto recompute;
978 case IPPROTO_MOBILE:
979 ifp->if_flags &= ~(IFF_LINK0|IFF_LINK2);
980 goto recompute;
981 default:
982 error = EPROTONOSUPPORT;
983 break;
984 }
985 break;
986 case GREGPROTO:
987 ifr->ifr_flags = sc->sc_proto;
988 break;
989 case GRESADDRS:
990 case GRESADDRD:
991 /*
992 * set tunnel endpoints, compute a less specific route
993 * to the remote end and mark if as up
994 */
995 sa = &ifr->ifr_addr;
996 if (cmd == GRESADDRS) {
997 sc->g_src = (satosin(sa))->sin_addr;
998 sc->g_srcport = satosin(sa)->sin_port;
999 }
1000 if (cmd == GRESADDRD) {
1001 if (sc->sc_proto == IPPROTO_UDP &&
1002 satosin(sa)->sin_port == 0) {
1003 error = EINVAL;
1004 break;
1005 }
1006 sc->g_dst = (satosin(sa))->sin_addr;
1007 sc->g_dstport = satosin(sa)->sin_port;
1008 }
1009 recompute:
1010 if (sc->sc_proto == IPPROTO_UDP ||
1011 (sc->g_src.s_addr != INADDR_ANY &&
1012 sc->g_dst.s_addr != INADDR_ANY)) {
1013 if (sc->sc_fp != NULL) {
1014 closef(sc->sc_fp, l);
1015 sc->sc_fp = NULL;
1016 }
1017 if (sc->route.ro_rt != NULL) {
1018 RTFREE(sc->route.ro_rt);
1019 sc->route.ro_rt = NULL;
1020 }
1021 if (sc->sc_proto == IPPROTO_UDP)
1022 error = gre_kick(sc);
1023 else if (gre_compute_route(sc) == 0)
1024 ifp->if_flags |= IFF_RUNNING;
1025 else
1026 ifp->if_flags &= ~IFF_RUNNING;
1027 }
1028 break;
1029 case GREGADDRS:
1030 memset(&si, 0, sizeof(si));
1031 si.sin_family = AF_INET;
1032 si.sin_len = sizeof(struct sockaddr_in);
1033 si.sin_addr.s_addr = sc->g_src.s_addr;
1034 sa = sintosa(&si);
1035 ifr->ifr_addr = *sa;
1036 break;
1037 case GREGADDRD:
1038 memset(&si, 0, sizeof(si));
1039 si.sin_family = AF_INET;
1040 si.sin_len = sizeof(struct sockaddr_in);
1041 si.sin_addr.s_addr = sc->g_dst.s_addr;
1042 sa = sintosa(&si);
1043 ifr->ifr_addr = *sa;
1044 break;
1045 case GREDSOCK:
1046 if (sc->sc_proto != IPPROTO_UDP)
1047 return EINVAL;
1048 if (sc->sc_fp != NULL) {
1049 closef(sc->sc_fp, l);
1050 sc->sc_fp = NULL;
1051 error = gre_kick(sc);
1052 }
1053 break;
1054 case GRESSOCK:
1055 if (sc->sc_proto != IPPROTO_UDP)
1056 return EINVAL;
1057 /* getsock() will FILE_USE() the descriptor for us */
1058 if ((error = getsock(p->p_fd, (int)ifr->ifr_value, &fp)) != 0)
1059 break;
1060 so = (struct socket *)fp->f_data;
1061 if (so->so_type != SOCK_DGRAM) {
1062 FILE_UNUSE(fp, NULL);
1063 error = EINVAL;
1064 break;
1065 }
1066 /* check address */
1067 if ((error = gre_getnames(so, curlwp, &src, &dst)) != 0) {
1068 FILE_UNUSE(fp, NULL);
1069 break;
1070 }
1071
1072 fp->f_count++;
1073
1074 ofp = sc->sc_fp;
1075 sc->sc_fp = fp;
1076 if ((error = gre_kick(sc)) != 0) {
1077 closef(fp, l);
1078 sc->sc_fp = ofp;
1079 break;
1080 }
1081 sc->g_src = src.sin_addr;
1082 sc->g_srcport = src.sin_port;
1083 sc->g_dst = dst.sin_addr;
1084 sc->g_dstport = dst.sin_port;
1085 if (ofp != NULL)
1086 closef(ofp, l);
1087 break;
1088 case SIOCSLIFPHYADDR:
1089 if (lifr->addr.ss_family != AF_INET ||
1090 lifr->dstaddr.ss_family != AF_INET) {
1091 error = EAFNOSUPPORT;
1092 break;
1093 }
1094 if (lifr->addr.ss_len != sizeof(si) ||
1095 lifr->dstaddr.ss_len != sizeof(si)) {
1096 error = EINVAL;
1097 break;
1098 }
1099 sc->g_src = satosin(&lifr->addr)->sin_addr;
1100 sc->g_dst = satosin(&lifr->dstaddr)->sin_addr;
1101 sc->g_srcport = satosin(&lifr->addr)->sin_port;
1102 sc->g_dstport = satosin(&lifr->dstaddr)->sin_port;
1103 goto recompute;
1104 case SIOCDIFPHYADDR:
1105 sc->g_src.s_addr = INADDR_ANY;
1106 sc->g_dst.s_addr = INADDR_ANY;
1107 sc->g_srcport = 0;
1108 sc->g_dstport = 0;
1109 goto recompute;
1110 case SIOCGLIFPHYADDR:
1111 if (sc->g_src.s_addr == INADDR_ANY ||
1112 sc->g_dst.s_addr == INADDR_ANY) {
1113 error = EADDRNOTAVAIL;
1114 break;
1115 }
1116 memset(&si, 0, sizeof(si));
1117 si.sin_family = AF_INET;
1118 si.sin_len = sizeof(struct sockaddr_in);
1119 si.sin_addr = sc->g_src;
1120 if (sc->sc_proto == IPPROTO_UDP)
1121 si.sin_port = sc->g_srcport;
1122 memcpy(&lifr->addr, &si, sizeof(si));
1123 si.sin_addr = sc->g_dst;
1124 if (sc->sc_proto == IPPROTO_UDP)
1125 si.sin_port = sc->g_dstport;
1126 memcpy(&lifr->dstaddr, &si, sizeof(si));
1127 break;
1128 default:
1129 error = EINVAL;
1130 break;
1131 }
1132 splx(s);
1133 return (error);
1134 }
1135
1136 /*
1137 * computes a route to our destination that is not the one
1138 * which would be taken by ip_output(), as this one will loop back to
1139 * us. If the interface is p2p as a--->b, then a routing entry exists
1140 * If we now send a packet to b (e.g. ping b), this will come down here
1141 * gets src=a, dst=b tacked on and would from ip_output() sent back to
1142 * if_gre.
1143 * Goal here is to compute a route to b that is less specific than
1144 * a-->b. We know that this one exists as in normal operation we have
1145 * at least a default route which matches.
1146 */
1147 static int
1148 gre_compute_route(struct gre_softc *sc)
1149 {
1150 struct route *ro;
1151
1152 ro = &sc->route;
1153
1154 memset(ro, 0, sizeof(struct route));
1155 satosin(&ro->ro_dst)->sin_addr = sc->g_dst;
1156 ro->ro_dst.sa_family = AF_INET;
1157 ro->ro_dst.sa_len = sizeof(ro->ro_dst);
1158
1159 /*
1160 * toggle last bit, so our interface is not found, but a less
1161 * specific route. I'd rather like to specify a shorter mask,
1162 * but this is not possible. Should work though. XXX
1163 * there is a simpler way ...
1164 */
1165 if ((sc->sc_if.if_flags & IFF_LINK1) == 0) {
1166 satosin(&ro->ro_dst)->sin_addr.s_addr
1167 = sc->g_dst.s_addr ^ htonl(0x1);
1168 }
1169
1170 #ifdef DIAGNOSTIC
1171 printf("%s: searching for a route to %s", sc->sc_if.if_xname,
1172 inet_ntoa(satosin(&ro->ro_dst)->sin_addr));
1173 #endif
1174
1175 rtalloc(ro);
1176
1177 /*
1178 * check if this returned a route at all and this route is no
1179 * recursion to ourself
1180 */
1181 if (ro->ro_rt == NULL || ro->ro_rt->rt_ifp->if_softc == sc) {
1182 #ifdef DIAGNOSTIC
1183 if (ro->ro_rt == NULL)
1184 printf(" - no route found!\n");
1185 else
1186 printf(" - route loops back to ourself!\n");
1187 #endif
1188 return EADDRNOTAVAIL;
1189 }
1190
1191 /*
1192 * now change it back - else ip_output will just drop
1193 * the route and search one to this interface ...
1194 */
1195 if ((sc->sc_if.if_flags & IFF_LINK1) == 0)
1196 satosin(&ro->ro_dst)->sin_addr = sc->g_dst;
1197
1198 #ifdef DIAGNOSTIC
1199 printf(", choosing %s with gateway %s\n", ro->ro_rt->rt_ifp->if_xname,
1200 inet_ntoa(satosin(ro->ro_rt->rt_gateway)->sin_addr));
1201 #endif
1202
1203 return 0;
1204 }
1205
1206 /*
1207 * do a checksum of a buffer - much like in_cksum, which operates on
1208 * mbufs.
1209 */
1210 u_int16_t
1211 gre_in_cksum(u_int16_t *p, u_int len)
1212 {
1213 u_int32_t sum = 0;
1214 int nwords = len >> 1;
1215
1216 while (nwords-- != 0)
1217 sum += *p++;
1218
1219 if (len & 1) {
1220 union {
1221 u_short w;
1222 u_char c[2];
1223 } u;
1224 u.c[0] = *(u_char *)p;
1225 u.c[1] = 0;
1226 sum += u.w;
1227 }
1228
1229 /* end-around-carry */
1230 sum = (sum >> 16) + (sum & 0xffff);
1231 sum += (sum >> 16);
1232 return (~sum);
1233 }
1234 #endif
1235
1236 void greattach(int);
1237
1238 /* ARGSUSED */
1239 void
1240 greattach(int count)
1241 {
1242 #ifdef INET
1243 LIST_INIT(&gre_softc_list);
1244 if_clone_attach(&gre_cloner);
1245 #endif
1246 }
1247