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