1 /* $OpenBSD: if_gre.c,v 1.34 2005/06/08 06:35:04 henning Exp $ */
2 /* $NetBSD: if_gre.c,v 1.9 1999/10/25 19:18:11 drochner Exp $ */
3
4 /*
5 * Copyright (c) 1998 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Heiko W.Rupp <hwr@pilhuhn.de>
10 *
11 * IPv6-over-GRE contributed by Gert Doering <gert@greenie.muc.de>
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgement:
23 * This product includes software developed by the NetBSD
24 * Foundation, Inc. and its contributors.
25 * 4. Neither the name of The NetBSD Foundation nor the names of its
26 * contributors may be used to endorse or promote products derived
27 * from this software without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
30 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
31 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
32 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
33 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
34 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
35 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
36 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
37 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39 * POSSIBILITY OF SUCH DAMAGE.
40 */
41
42 /*
43 * Encapsulate L3 protocols into IP, per RFC 1701 and 1702.
44 * See gre(4) for more details.
45 * Also supported: IP in IP encapsulation (proto 55) per RFC 2004.
46 */
47
48 #include "gre.h"
49 #if NGRE > 0
50
51 #include "bpfilter.h"
52
53 #include <sys/param.h>
54 #include <sys/proc.h>
55 #include <sys/mbuf.h>
56 #include <sys/socket.h>
57 #include <sys/sockio.h>
58 #include <sys/kernel.h>
59 #include <sys/systm.h>
60
61 #include <net/if.h>
62 #include <net/if_types.h>
63 #include <net/netisr.h>
64 #include <net/route.h>
65
66 #ifdef INET
67 #include <netinet/in.h>
68 #include <netinet/in_systm.h>
69 #include <netinet/in_var.h>
70 #include <netinet/ip.h>
71 #include <netinet/ip_var.h>
72 #include <netinet/if_ether.h>
73 #else
74 #error "if_gre used without inet"
75 #endif
76
77 #ifdef NETATALK
78 #include <netatalk/at.h>
79 #include <netatalk/at_var.h>
80 #include <netatalk/at_extern.h>
81 #endif
82
83 #if NBPFILTER > 0
84 #include <net/bpf.h>
85 #endif
86
87 #include <net/if_gre.h>
88
89 #ifndef GRE_RECURSION_LIMIT
90 #define GRE_RECURSION_LIMIT 3 /* How many levels of recursion allowed */
91 #endif /* GRE_RECURSION_LIMIT */
92
93 /*
94 * It is not easy to calculate the right value for a GRE MTU.
95 * We leave this task to the admin and use the same default that
96 * other vendors use.
97 */
98 #define GREMTU 1476
99
100 int gre_clone_create(struct if_clone *, int);
101 int gre_clone_destroy(struct ifnet *);
102
103 struct gre_softc_head gre_softc_list;
104 struct if_clone gre_cloner =
105 IF_CLONE_INITIALIZER("gre", gre_clone_create, gre_clone_destroy);
106
107 /*
108 * We can control the acceptance of GRE and MobileIP packets by
109 * altering the sysctl net.inet.gre.allow and net.inet.mobileip.allow values
110 * respectively. Zero means drop them, all else is acceptance. We can also
111 * control acceptance of WCCPv1-style GRE packets through the
112 * net.inet.gre.wccp value, but be aware it depends upon normal GRE being
113 * allowed as well.
114 *
115 */
116 int gre_allow = 0;
117 int gre_wccp = 0;
118 int ip_mobile_allow = 0;
119
120 static void gre_compute_route(struct gre_softc *sc);
121
122 void
greattach(int n)123 greattach(int n)
124 {
125 LIST_INIT(&gre_softc_list);
126 if_clone_attach(&gre_cloner);
127 }
128
129 int
gre_clone_create(struct if_clone * ifc,int unit)130 gre_clone_create(struct if_clone *ifc, int unit)
131 {
132 struct gre_softc *sc;
133 int s;
134
135 sc = malloc(sizeof(*sc), M_DEVBUF, M_NOWAIT);
136 if (!sc)
137 return (ENOMEM);
138 bzero(sc, sizeof(*sc));
139 snprintf(sc->sc_if.if_xname, sizeof sc->sc_if.if_xname, "%s%d",
140 ifc->ifc_name, unit);
141 sc->sc_if.if_softc = sc;
142 sc->sc_if.if_type = IFT_TUNNEL;
143 sc->sc_if.if_addrlen = 0;
144 sc->sc_if.if_hdrlen = 24; /* IP + GRE */
145 sc->sc_if.if_mtu = GREMTU;
146 sc->sc_if.if_flags = IFF_POINTOPOINT|IFF_MULTICAST;
147 sc->sc_if.if_output = gre_output;
148 sc->sc_if.if_ioctl = gre_ioctl;
149 sc->sc_if.if_collisions = 0;
150 sc->sc_if.if_ierrors = 0;
151 sc->sc_if.if_oerrors = 0;
152 sc->sc_if.if_ipackets = 0;
153 sc->sc_if.if_opackets = 0;
154 sc->g_dst.s_addr = sc->g_src.s_addr = INADDR_ANY;
155 sc->g_proto = IPPROTO_GRE;
156 sc->sc_if.if_flags |= IFF_LINK0;
157
158 if_attach(&sc->sc_if);
159 if_alloc_sadl(&sc->sc_if);
160
161 #if NBPFILTER > 0
162 bpfattach(&sc->sc_if.if_bpf, &sc->sc_if, DLT_NULL,
163 sizeof(u_int32_t));
164 #endif
165 s = splnet();
166 LIST_INSERT_HEAD(&gre_softc_list, sc, sc_list);
167 splx(s);
168
169 return (0);
170 }
171
172 int
gre_clone_destroy(struct ifnet * ifp)173 gre_clone_destroy(struct ifnet *ifp)
174 {
175 struct gre_softc *sc = ifp->if_softc;
176 int s;
177
178 s = splnet();
179 LIST_REMOVE(sc, sc_list);
180 splx(s);
181
182 #if NBPFILTER > 0
183 bpfdetach(ifp);
184 #endif
185 if_detach(ifp);
186
187 free(sc, M_DEVBUF);
188 return (0);
189 }
190
191 /*
192 * The output routine. Takes a packet and encapsulates it in the protocol
193 * given by sc->g_proto. See also RFC 1701 and RFC 2004.
194 */
195
196 int
gre_output(struct ifnet * ifp,struct mbuf * m,struct sockaddr * dst,struct rtentry * rt)197 gre_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
198 struct rtentry *rt)
199 {
200 int error = 0;
201 struct gre_softc *sc = (struct gre_softc *) (ifp->if_softc);
202 struct greip *gh = NULL;
203 struct ip *inp = NULL;
204 u_int8_t ip_tos = 0;
205 u_int16_t etype = 0;
206 struct mobile_h mob_h;
207 struct m_tag *mtag;
208
209 if ((ifp->if_flags & IFF_UP) == 0 ||
210 sc->g_src.s_addr == INADDR_ANY || sc->g_dst.s_addr == INADDR_ANY) {
211 m_freem(m);
212 error = ENETDOWN;
213 goto end;
214 }
215
216 /* Try to limit infinite recursion through misconfiguration. */
217 for (mtag = m_tag_find(m, PACKET_TAG_GRE, NULL); mtag;
218 mtag = m_tag_find(m, PACKET_TAG_GRE, mtag)) {
219 if (!bcmp((caddr_t)(mtag + 1), &ifp, sizeof(struct ifnet *))) {
220 IF_DROP(&ifp->if_snd);
221 m_freem(m);
222 error = EIO;
223 goto end;
224 }
225 }
226
227 mtag = m_tag_get(PACKET_TAG_GRE, sizeof(struct ifnet *), M_NOWAIT);
228 if (mtag == NULL) {
229 IF_DROP(&ifp->if_snd);
230 m_freem(m);
231 error = ENOBUFS;
232 goto end;
233 }
234 bcopy(&ifp, (caddr_t)(mtag + 1), sizeof(struct ifnet *));
235 m_tag_prepend(m, mtag);
236
237 #if NBPFILTER >0
238 if (ifp->if_bpf) {
239 /*
240 * We need to prepend the address family as a four
241 * byte field. Cons up a fake header to pacify bpf.
242 * This is safe because bpf will only read from the
243 * mbuf (i.e., it won't try to free it or keep a
244 * pointer a to it).
245 */
246 struct mbuf m0;
247 u_int32_t af = dst->sa_family;
248
249 m0.m_flags = 0;
250 m0.m_next = m;
251 m0.m_len = 4;
252 m0.m_data = (char *) ⁡
253
254 bpf_mtap(ifp->if_bpf, &m0);
255 }
256 #endif
257
258 if (sc->g_proto == IPPROTO_MOBILE) {
259 if (ip_mobile_allow == 0) {
260 IF_DROP(&ifp->if_snd);
261 m_freem(m);
262 error = EACCES;
263 goto end;
264 }
265
266 if (dst->sa_family == AF_INET) {
267 struct mbuf *m0;
268 int msiz;
269
270 /*
271 * Make sure the complete IP header (with options)
272 * is in the first mbuf.
273 */
274 if (m->m_len < sizeof(struct ip)) {
275 m = m_pullup(m, sizeof(struct ip));
276 if (m == NULL) {
277 IF_DROP(&ifp->if_snd);
278 error = ENOBUFS;
279 goto end;
280 } else
281 inp = mtod(m, struct ip *);
282
283 if (m->m_len < inp->ip_hl << 2) {
284 m = m_pullup(m,
285 sizeof(inp->ip_hl << 2));
286 if (m == NULL) {
287 IF_DROP(&ifp->if_snd);
288 error = ENOBUFS;
289 goto end;
290 }
291 }
292 }
293
294 inp = mtod(m, struct ip *);
295
296 bzero(&mob_h, MOB_H_SIZ_L);
297 mob_h.proto = (inp->ip_p) << 8;
298 mob_h.odst = inp->ip_dst.s_addr;
299 inp->ip_dst.s_addr = sc->g_dst.s_addr;
300
301 /*
302 * If the packet comes from our host, we only change
303 * the destination address in the IP header.
304 * Otherwise we need to save and change the source.
305 */
306 if (inp->ip_src.s_addr == sc->g_src.s_addr) {
307 msiz = MOB_H_SIZ_S;
308 } else {
309 mob_h.proto |= MOB_H_SBIT;
310 mob_h.osrc = inp->ip_src.s_addr;
311 inp->ip_src.s_addr = sc->g_src.s_addr;
312 msiz = MOB_H_SIZ_L;
313 }
314
315 HTONS(mob_h.proto);
316 mob_h.hcrc = gre_in_cksum((u_int16_t *) &mob_h, msiz);
317
318 /* Squeeze in the mobility header */
319 if ((m->m_data - msiz) < m->m_pktdat) {
320 /* Need new mbuf */
321 MGETHDR(m0, M_DONTWAIT, MT_HEADER);
322 if (m0 == NULL) {
323 IF_DROP(&ifp->if_snd);
324 m_freem(m);
325 error = ENOBUFS;
326 goto end;
327 }
328 M_MOVE_HDR(m0, m);
329
330 m0->m_len = msiz + (inp->ip_hl << 2);
331 m0->m_data += max_linkhdr;
332 m0->m_pkthdr.len = m->m_pkthdr.len + msiz;
333 m->m_data += inp->ip_hl << 2;
334 m->m_len -= inp->ip_hl << 2;
335
336 bcopy((caddr_t) inp, mtod(m0, caddr_t),
337 sizeof(struct ip));
338
339 m0->m_next = m;
340 m = m0;
341 } else { /* we have some space left in the old one */
342 m->m_data -= msiz;
343 m->m_len += msiz;
344 m->m_pkthdr.len += msiz;
345 bcopy(inp, mtod(m, caddr_t),
346 inp->ip_hl << 2);
347 }
348
349 /* Copy Mobility header */
350 inp = mtod(m, struct ip *);
351 bcopy(&mob_h, (caddr_t)(inp + 1), (unsigned) msiz);
352 inp->ip_len = htons(ntohs(inp->ip_len) + msiz);
353 } else { /* AF_INET */
354 IF_DROP(&ifp->if_snd);
355 m_freem(m);
356 error = EINVAL;
357 goto end;
358 }
359 } else if (sc->g_proto == IPPROTO_GRE) {
360 if (gre_allow == 0) {
361 IF_DROP(&ifp->if_snd);
362 m_freem(m);
363 error = EACCES;
364 goto end;
365 }
366
367 switch(dst->sa_family) {
368 case AF_INET:
369 if (m->m_len < sizeof(struct ip)) {
370 m = m_pullup(m, sizeof(struct ip));
371 if (m == NULL) {
372 IF_DROP(&ifp->if_snd);
373 error = ENOBUFS;
374 goto end;
375 }
376 }
377
378 inp = mtod(m, struct ip *);
379 ip_tos = inp->ip_tos;
380 etype = ETHERTYPE_IP;
381 break;
382 #ifdef NETATALK
383 case AF_APPLETALK:
384 etype = ETHERTYPE_AT;
385 break;
386 #endif
387 #ifdef INET6
388 case AF_INET6:
389 etype = ETHERTYPE_IPV6;
390 break;
391 #endif
392 default:
393 IF_DROP(&ifp->if_snd);
394 m_freem(m);
395 error = EAFNOSUPPORT;
396 goto end;
397 }
398
399 M_PREPEND(m, sizeof(struct greip), M_DONTWAIT);
400 } else {
401 IF_DROP(&ifp->if_snd);
402 m_freem(m);
403 error = EINVAL;
404 goto end;
405 }
406
407 if (m == NULL) {
408 IF_DROP(&ifp->if_snd);
409 error = ENOBUFS;
410 goto end;
411 }
412
413 gh = mtod(m, struct greip *);
414 if (sc->g_proto == IPPROTO_GRE) {
415 /* We don't support any GRE flags for now */
416
417 bzero((void *) &gh->gi_g, sizeof(struct gre_h));
418 gh->gi_ptype = htons(etype);
419 }
420
421 gh->gi_pr = sc->g_proto;
422 if (sc->g_proto != IPPROTO_MOBILE) {
423 gh->gi_src = sc->g_src;
424 gh->gi_dst = sc->g_dst;
425 ((struct ip *) gh)->ip_hl = (sizeof(struct ip)) >> 2;
426 ((struct ip *) gh)->ip_ttl = ip_defttl;
427 ((struct ip *) gh)->ip_tos = ip_tos;
428 gh->gi_len = htons(m->m_pkthdr.len);
429 }
430
431 ifp->if_opackets++;
432 ifp->if_obytes += m->m_pkthdr.len;
433
434 /* Send it off */
435 error = ip_output(m, (void *)NULL, &sc->route, 0, (void *)NULL, (void *)NULL);
436 end:
437 if (error)
438 ifp->if_oerrors++;
439 return (error);
440 }
441
442 int
gre_ioctl(struct ifnet * ifp,u_long cmd,caddr_t data)443 gre_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
444 {
445
446 struct ifreq *ifr = (struct ifreq *) data;
447 struct if_laddrreq *lifr = (struct if_laddrreq *)data;
448 struct gre_softc *sc = ifp->if_softc;
449 int s;
450 struct sockaddr_in si;
451 struct sockaddr *sa = NULL;
452 int error = 0;
453 struct proc *prc = curproc; /* XXX */
454
455 s = splimp();
456 switch(cmd) {
457 case SIOCSIFADDR:
458 ifp->if_flags |= IFF_UP;
459 break;
460 case SIOCSIFDSTADDR:
461 break;
462 case SIOCSIFFLAGS:
463 if ((ifr->ifr_flags & IFF_LINK0) != 0)
464 sc->g_proto = IPPROTO_GRE;
465 else
466 sc->g_proto = IPPROTO_MOBILE;
467 break;
468 case SIOCSIFMTU:
469 if (ifr->ifr_mtu < 576) {
470 error = EINVAL;
471 break;
472 }
473 ifp->if_mtu = ifr->ifr_mtu;
474 break;
475 case SIOCGIFMTU:
476 ifr->ifr_mtu = sc->sc_if.if_mtu;
477 break;
478 case SIOCADDMULTI:
479 case SIOCDELMULTI:
480 if (ifr == 0) {
481 error = EAFNOSUPPORT;
482 break;
483 }
484 switch (ifr->ifr_addr.sa_family) {
485 #ifdef INET
486 case AF_INET:
487 break;
488 #endif
489 #ifdef INET6
490 case AF_INET6:
491 break;
492 #endif
493 default:
494 error = EAFNOSUPPORT;
495 break;
496 }
497 break;
498 case GRESPROTO:
499 /* Check for superuser */
500 if ((error = suser(prc, 0)) != 0)
501 break;
502
503 sc->g_proto = ifr->ifr_flags;
504 switch (sc->g_proto) {
505 case IPPROTO_GRE:
506 ifp->if_flags |= IFF_LINK0;
507 break;
508 case IPPROTO_MOBILE:
509 ifp->if_flags &= ~IFF_LINK0;
510 break;
511 default:
512 error = EPROTONOSUPPORT;
513 break;
514 }
515 break;
516 case GREGPROTO:
517 ifr->ifr_flags = sc->g_proto;
518 break;
519 case GRESADDRS:
520 case GRESADDRD:
521 /* Check for superuser */
522 if ((error = suser(prc, 0)) != 0)
523 break;
524
525 /*
526 * set tunnel endpoints, compute a less specific route
527 * to the remote end and mark if as up
528 */
529 sa = &ifr->ifr_addr;
530 if (cmd == GRESADDRS )
531 sc->g_src = (satosin(sa))->sin_addr;
532 if (cmd == GRESADDRD )
533 sc->g_dst = (satosin(sa))->sin_addr;
534 recompute:
535 if ((sc->g_src.s_addr != INADDR_ANY) &&
536 (sc->g_dst.s_addr != INADDR_ANY)) {
537 if (sc->route.ro_rt != 0) {
538 /* free old route */
539 RTFREE(sc->route.ro_rt);
540 sc->route.ro_rt = (struct rtentry *) 0;
541 }
542
543 gre_compute_route(sc);
544 if (sc->route.ro_rt == 0)
545 {
546 sc->g_src.s_addr = INADDR_ANY;
547 sc->g_dst.s_addr = INADDR_ANY;
548 splx(s);
549 return EIO; /* Is this is good ? */
550 }
551 ifp->if_flags |= IFF_UP;
552 }
553 break;
554 case GREGADDRS:
555 bzero(&si, sizeof(si));
556 si.sin_family = AF_INET;
557 si.sin_len = sizeof(struct sockaddr_in);
558 si.sin_addr.s_addr = sc->g_src.s_addr;
559 sa = sintosa(&si);
560 ifr->ifr_addr = *sa;
561 break;
562 case GREGADDRD:
563 bzero(&si, sizeof(si));
564 si.sin_family = AF_INET;
565 si.sin_len = sizeof(struct sockaddr_in);
566 si.sin_addr.s_addr = sc->g_dst.s_addr;
567 sa = sintosa(&si);
568 ifr->ifr_addr = *sa;
569 break;
570 case SIOCSLIFPHYADDR:
571 if ((error = suser(prc, 0)) != 0)
572 break;
573 if (lifr->addr.ss_family != AF_INET ||
574 lifr->dstaddr.ss_family != AF_INET) {
575 error = EAFNOSUPPORT;
576 break;
577 }
578 if (lifr->addr.ss_len != sizeof(si) ||
579 lifr->dstaddr.ss_len != sizeof(si)) {
580 error = EINVAL;
581 break;
582 }
583 sc->g_src = (satosin((struct sockadrr *)&lifr->addr))->sin_addr;
584 sc->g_dst =
585 (satosin((struct sockadrr *)&lifr->dstaddr))->sin_addr;
586 goto recompute;
587 case SIOCDIFPHYADDR:
588 if ((error = suser(prc, 0)) != 0)
589 break;
590 sc->g_src.s_addr = INADDR_ANY;
591 sc->g_dst.s_addr = INADDR_ANY;
592 break;
593 case SIOCGLIFPHYADDR:
594 if (sc->g_src.s_addr == INADDR_ANY ||
595 sc->g_dst.s_addr == INADDR_ANY) {
596 error = EADDRNOTAVAIL;
597 break;
598 }
599 bzero(&si, sizeof(si));
600 si.sin_family = AF_INET;
601 si.sin_len = sizeof(struct sockaddr_in);
602 si.sin_addr.s_addr = sc->g_src.s_addr;
603 memcpy(&lifr->addr, &si, sizeof(si));
604 si.sin_addr.s_addr = sc->g_dst.s_addr;
605 memcpy(&lifr->dstaddr, &si, sizeof(si));
606 break;
607 default:
608 error = EINVAL;
609 }
610
611 splx(s);
612 return (error);
613 }
614
615 /*
616 * computes a route to our destination that is not the one
617 * which would be taken by ip_output(), as this one will loop back to
618 * us. If the interface is p2p as a--->b, then a routing entry exists
619 * If we now send a packet to b (e.g. ping b), this will come down here
620 * gets src=a, dst=b tacked on and would from ip_output() sent back to
621 * if_gre.
622 * Goal here is to compute a route to b that is less specific than
623 * a-->b. We know that this one exists as in normal operation we have
624 * at least a default route which matches.
625 */
626
627 static void
gre_compute_route(struct gre_softc * sc)628 gre_compute_route(struct gre_softc *sc)
629 {
630 struct route *ro;
631 u_int32_t a, b, c;
632
633 ro = &sc->route;
634
635 bzero(ro, sizeof(struct route));
636 ((struct sockaddr_in *) &ro->ro_dst)->sin_addr = sc->g_dst;
637 ro->ro_dst.sa_family = AF_INET;
638 ro->ro_dst.sa_len = sizeof(ro->ro_dst);
639
640 /*
641 * toggle last bit, so our interface is not found, but a less
642 * specific route. I'd rather like to specify a shorter mask,
643 * but this is not possible. Should work though. XXX
644 * there is a simpler way ...
645 */
646 if ((sc->sc_if.if_flags & IFF_LINK1) == 0) {
647 a = ntohl(sc->g_dst.s_addr);
648 b = a & 0x01;
649 c = a & 0xfffffffe;
650 b = b ^ 0x01;
651 a = b | c;
652 ((struct sockaddr_in *) &ro->ro_dst)->sin_addr.s_addr = htonl(a);
653 }
654
655 rtalloc(ro);
656 if (ro->ro_rt == 0)
657 return;
658
659 /*
660 * Check whether we just created a loop. An even more paranoid
661 * check would be against all GRE interfaces, but that would
662 * not allow people to link GRE tunnels.
663 */
664 if (ro->ro_rt->rt_ifp == &sc->sc_if) {
665 RTFREE(ro->ro_rt);
666 ro->ro_rt = (struct rtentry *) 0;
667 return;
668 }
669
670 /*
671 * now change it back - else ip_output will just drop
672 * the route and search one to this interface ...
673 */
674 if ((sc->sc_if.if_flags & IFF_LINK1) == 0)
675 ((struct sockaddr_in *) &ro->ro_dst)->sin_addr = sc->g_dst;
676 }
677
678 /*
679 * do a checksum of a buffer - much like in_cksum, which operates on
680 * mbufs.
681 */
682 u_int16_t
gre_in_cksum(u_int16_t * p,u_int len)683 gre_in_cksum(u_int16_t *p, u_int len)
684 {
685 u_int32_t sum = 0;
686 int nwords = len >> 1;
687
688 while (nwords-- != 0)
689 sum += *p++;
690
691 if (len & 1) {
692 union {
693 u_short w;
694 u_char c[2];
695 } u;
696 u.c[0] = *(u_char *) p;
697 u.c[1] = 0;
698 sum += u.w;
699 }
700
701 /* end-around-carry */
702 sum = (sum >> 16) + (sum & 0xffff);
703 sum += (sum >> 16);
704 return (~sum);
705 }
706 #endif
707