if_gre.c revision 1.55 1 1.55 perry /* $NetBSD: if_gre.c,v 1.55 2005/02/26 22:45:09 perry Exp $ */
2 1.4 thorpej
3 1.1 hwr /*
4 1.4 thorpej * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 1.1 hwr * All rights reserved.
6 1.1 hwr *
7 1.1 hwr * This code is derived from software contributed to The NetBSD Foundation
8 1.1 hwr * by Heiko W.Rupp <hwr (at) pilhuhn.de>
9 1.1 hwr *
10 1.1 hwr * Redistribution and use in source and binary forms, with or without
11 1.1 hwr * modification, are permitted provided that the following conditions
12 1.1 hwr * are met:
13 1.1 hwr * 1. Redistributions of source code must retain the above copyright
14 1.1 hwr * notice, this list of conditions and the following disclaimer.
15 1.1 hwr * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 hwr * notice, this list of conditions and the following disclaimer in the
17 1.1 hwr * documentation and/or other materials provided with the distribution.
18 1.1 hwr * 3. All advertising materials mentioning features or use of this software
19 1.1 hwr * must display the following acknowledgement:
20 1.1 hwr * This product includes software developed by the NetBSD
21 1.1 hwr * Foundation, Inc. and its contributors.
22 1.1 hwr * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 hwr * contributors may be used to endorse or promote products derived
24 1.1 hwr * from this software without specific prior written permission.
25 1.1 hwr *
26 1.1 hwr * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 hwr * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 hwr * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 hwr * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 hwr * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 hwr * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 hwr * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 hwr * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 hwr * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 hwr * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 hwr * POSSIBILITY OF SUCH DAMAGE.
37 1.1 hwr */
38 1.1 hwr
39 1.1 hwr /*
40 1.1 hwr * Encapsulate L3 protocols into IP
41 1.1 hwr * See RFC 1701 and 1702 for more details.
42 1.1 hwr * If_gre is compatible with Cisco GRE tunnels, so you can
43 1.1 hwr * have a NetBSD box as the other end of a tunnel interface of a Cisco
44 1.1 hwr * router. See gre(4) for more details.
45 1.6 hwr * Also supported: IP in IP encaps (proto 55) as of RFC 2004
46 1.1 hwr */
47 1.22 lukem
48 1.22 lukem #include <sys/cdefs.h>
49 1.55 perry __KERNEL_RCSID(0, "$NetBSD: if_gre.c,v 1.55 2005/02/26 22:45:09 perry Exp $");
50 1.1 hwr
51 1.1 hwr #include "opt_inet.h"
52 1.9 drochner #include "opt_ns.h"
53 1.1 hwr #include "bpfilter.h"
54 1.1 hwr
55 1.54 christos #ifdef INET
56 1.1 hwr #include <sys/param.h>
57 1.1 hwr #include <sys/malloc.h>
58 1.1 hwr #include <sys/mbuf.h>
59 1.13 martin #include <sys/proc.h>
60 1.1 hwr #include <sys/protosw.h>
61 1.1 hwr #include <sys/socket.h>
62 1.1 hwr #include <sys/ioctl.h>
63 1.10 thorpej #include <sys/queue.h>
64 1.1 hwr #if __NetBSD__
65 1.1 hwr #include <sys/systm.h>
66 1.1 hwr #endif
67 1.1 hwr
68 1.1 hwr #include <machine/cpu.h>
69 1.1 hwr
70 1.1 hwr #include <net/ethertypes.h>
71 1.1 hwr #include <net/if.h>
72 1.1 hwr #include <net/if_types.h>
73 1.1 hwr #include <net/netisr.h>
74 1.1 hwr #include <net/route.h>
75 1.1 hwr
76 1.1 hwr #ifdef INET
77 1.1 hwr #include <netinet/in.h>
78 1.1 hwr #include <netinet/in_systm.h>
79 1.1 hwr #include <netinet/in_var.h>
80 1.1 hwr #include <netinet/ip.h>
81 1.1 hwr #include <netinet/ip_var.h>
82 1.1 hwr #else
83 1.4 thorpej #error "Huh? if_gre without inet?"
84 1.1 hwr #endif
85 1.1 hwr
86 1.1 hwr #ifdef NS
87 1.1 hwr #include <netns/ns.h>
88 1.1 hwr #include <netns/ns_if.h>
89 1.1 hwr #endif
90 1.1 hwr
91 1.1 hwr #ifdef NETATALK
92 1.1 hwr #include <netatalk/at.h>
93 1.1 hwr #include <netatalk/at_var.h>
94 1.1 hwr #include <netatalk/at_extern.h>
95 1.1 hwr #endif
96 1.1 hwr
97 1.1 hwr #if NBPFILTER > 0
98 1.1 hwr #include <sys/time.h>
99 1.1 hwr #include <net/bpf.h>
100 1.1 hwr #endif
101 1.1 hwr
102 1.1 hwr #include <net/if_gre.h>
103 1.1 hwr
104 1.20 itojun /*
105 1.27 martin * It is not easy to calculate the right value for a GRE MTU.
106 1.27 martin * We leave this task to the admin and use the same default that
107 1.27 martin * other vendors use.
108 1.20 itojun */
109 1.27 martin #define GREMTU 1476
110 1.1 hwr
111 1.11 thorpej struct gre_softc_head gre_softc_list;
112 1.26 martin int ip_gre_ttl = GRE_TTL;
113 1.1 hwr
114 1.10 thorpej int gre_clone_create __P((struct if_clone *, int));
115 1.53 peter int gre_clone_destroy __P((struct ifnet *));
116 1.10 thorpej
117 1.10 thorpej struct if_clone gre_cloner =
118 1.10 thorpej IF_CLONE_INITIALIZER("gre", gre_clone_create, gre_clone_destroy);
119 1.1 hwr
120 1.25 martin int gre_compute_route(struct gre_softc *sc);
121 1.1 hwr
122 1.10 thorpej int
123 1.10 thorpej gre_clone_create(ifc, unit)
124 1.10 thorpej struct if_clone *ifc;
125 1.10 thorpej int unit;
126 1.1 hwr {
127 1.8 explorer struct gre_softc *sc;
128 1.1 hwr
129 1.10 thorpej sc = malloc(sizeof(struct gre_softc), M_DEVBUF, M_WAITOK);
130 1.10 thorpej memset(sc, 0, sizeof(struct gre_softc));
131 1.10 thorpej
132 1.50 itojun snprintf(sc->sc_if.if_xname, sizeof(sc->sc_if.if_xname), "%s%d",
133 1.50 itojun ifc->ifc_name, unit);
134 1.10 thorpej sc->sc_if.if_softc = sc;
135 1.51 tron sc->sc_if.if_type = IFT_TUNNEL;
136 1.34 itojun sc->sc_if.if_addrlen = 0;
137 1.10 thorpej sc->sc_if.if_hdrlen = 24; /* IP + GRE */
138 1.15 thorpej sc->sc_if.if_dlt = DLT_NULL;
139 1.20 itojun sc->sc_if.if_mtu = GREMTU;
140 1.10 thorpej sc->sc_if.if_flags = IFF_POINTOPOINT|IFF_MULTICAST;
141 1.10 thorpej sc->sc_if.if_output = gre_output;
142 1.10 thorpej sc->sc_if.if_ioctl = gre_ioctl;
143 1.10 thorpej sc->g_dst.s_addr = sc->g_src.s_addr = INADDR_ANY;
144 1.10 thorpej sc->g_proto = IPPROTO_GRE;
145 1.35 itojun sc->sc_if.if_flags |= IFF_LINK0;
146 1.10 thorpej if_attach(&sc->sc_if);
147 1.16 thorpej if_alloc_sadl(&sc->sc_if);
148 1.1 hwr #if NBPFILTER > 0
149 1.14 thorpej bpfattach(&sc->sc_if, DLT_NULL, sizeof(u_int32_t));
150 1.1 hwr #endif
151 1.10 thorpej LIST_INSERT_HEAD(&gre_softc_list, sc, sc_list);
152 1.10 thorpej return (0);
153 1.10 thorpej }
154 1.1 hwr
155 1.53 peter int
156 1.10 thorpej gre_clone_destroy(ifp)
157 1.10 thorpej struct ifnet *ifp;
158 1.10 thorpej {
159 1.10 thorpej struct gre_softc *sc = ifp->if_softc;
160 1.10 thorpej
161 1.10 thorpej LIST_REMOVE(sc, sc_list);
162 1.10 thorpej #if NBPFILTER > 0
163 1.10 thorpej bpfdetach(ifp);
164 1.10 thorpej #endif
165 1.10 thorpej if_detach(ifp);
166 1.10 thorpej free(sc, M_DEVBUF);
167 1.53 peter
168 1.53 peter return (0);
169 1.1 hwr }
170 1.1 hwr
171 1.20 itojun /*
172 1.1 hwr * The output routine. Takes a packet and encapsulates it in the protocol
173 1.6 hwr * given by sc->g_proto. See also RFC 1701 and RFC 2004
174 1.1 hwr */
175 1.1 hwr int
176 1.8 explorer gre_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
177 1.8 explorer struct rtentry *rt)
178 1.1 hwr {
179 1.8 explorer int error = 0;
180 1.10 thorpej struct gre_softc *sc = ifp->if_softc;
181 1.1 hwr struct greip *gh;
182 1.41 itojun struct ip *ip;
183 1.48 itojun u_int16_t etype = 0;
184 1.20 itojun struct mobile_h mob_h;
185 1.24 martin
186 1.39 itojun if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == 0 ||
187 1.37 itojun sc->g_src.s_addr == INADDR_ANY || sc->g_dst.s_addr == INADDR_ANY) {
188 1.37 itojun m_freem(m);
189 1.38 itojun error = ENETDOWN;
190 1.38 itojun goto end;
191 1.37 itojun }
192 1.1 hwr
193 1.8 explorer gh = NULL;
194 1.41 itojun ip = NULL;
195 1.1 hwr
196 1.1 hwr #if NBPFILTER >0
197 1.52 christos if (ifp->if_bpf)
198 1.52 christos bpf_mtap_af(ifp->if_bpf, dst->sa_family, m);
199 1.1 hwr #endif
200 1.1 hwr
201 1.26 martin m->m_flags &= ~(M_BCAST|M_MCAST);
202 1.1 hwr
203 1.5 thorpej if (sc->g_proto == IPPROTO_MOBILE) {
204 1.3 hwr if (dst->sa_family == AF_INET) {
205 1.3 hwr struct mbuf *m0;
206 1.3 hwr int msiz;
207 1.3 hwr
208 1.41 itojun ip = mtod(m, struct ip *);
209 1.3 hwr
210 1.8 explorer memset(&mob_h, 0, MOB_H_SIZ_L);
211 1.41 itojun mob_h.proto = (ip->ip_p) << 8;
212 1.41 itojun mob_h.odst = ip->ip_dst.s_addr;
213 1.41 itojun ip->ip_dst.s_addr = sc->g_dst.s_addr;
214 1.3 hwr
215 1.3 hwr /*
216 1.8 explorer * If the packet comes from our host, we only change
217 1.8 explorer * the destination address in the IP header.
218 1.8 explorer * Else we also need to save and change the source
219 1.3 hwr */
220 1.41 itojun if (in_hosteq(ip->ip_src, sc->g_src)) {
221 1.8 explorer msiz = MOB_H_SIZ_S;
222 1.3 hwr } else {
223 1.3 hwr mob_h.proto |= MOB_H_SBIT;
224 1.41 itojun mob_h.osrc = ip->ip_src.s_addr;
225 1.41 itojun ip->ip_src.s_addr = sc->g_src.s_addr;
226 1.8 explorer msiz = MOB_H_SIZ_L;
227 1.3 hwr }
228 1.3 hwr HTONS(mob_h.proto);
229 1.48 itojun mob_h.hcrc = gre_in_cksum((u_int16_t *)&mob_h, msiz);
230 1.3 hwr
231 1.3 hwr if ((m->m_data - msiz) < m->m_pktdat) {
232 1.3 hwr /* need new mbuf */
233 1.3 hwr MGETHDR(m0, M_DONTWAIT, MT_HEADER);
234 1.8 explorer if (m0 == NULL) {
235 1.3 hwr IF_DROP(&ifp->if_snd);
236 1.3 hwr m_freem(m);
237 1.38 itojun error = ENOBUFS;
238 1.38 itojun goto end;
239 1.3 hwr }
240 1.8 explorer m0->m_next = m;
241 1.3 hwr m->m_data += sizeof(struct ip);
242 1.3 hwr m->m_len -= sizeof(struct ip);
243 1.8 explorer m0->m_pkthdr.len = m->m_pkthdr.len + msiz;
244 1.3 hwr m0->m_len = msiz + sizeof(struct ip);
245 1.3 hwr m0->m_data += max_linkhdr;
246 1.41 itojun memcpy(mtod(m0, caddr_t), (caddr_t)ip,
247 1.8 explorer sizeof(struct ip));
248 1.8 explorer m = m0;
249 1.20 itojun } else { /* we have some space left in the old one */
250 1.8 explorer m->m_data -= msiz;
251 1.8 explorer m->m_len += msiz;
252 1.8 explorer m->m_pkthdr.len += msiz;
253 1.41 itojun memmove(mtod(m, caddr_t), ip,
254 1.8 explorer sizeof(struct ip));
255 1.3 hwr }
256 1.41 itojun ip = mtod(m, struct ip *);
257 1.41 itojun memcpy((caddr_t)(ip + 1), &mob_h, (unsigned)msiz);
258 1.42 itojun ip->ip_len = htons(ntohs(ip->ip_len) + msiz);
259 1.3 hwr } else { /* AF_INET */
260 1.3 hwr IF_DROP(&ifp->if_snd);
261 1.3 hwr m_freem(m);
262 1.38 itojun error = EINVAL;
263 1.38 itojun goto end;
264 1.3 hwr }
265 1.1 hwr } else if (sc->g_proto == IPPROTO_GRE) {
266 1.20 itojun switch (dst->sa_family) {
267 1.1 hwr case AF_INET:
268 1.41 itojun ip = mtod(m, struct ip *);
269 1.8 explorer etype = ETHERTYPE_IP;
270 1.1 hwr break;
271 1.1 hwr #ifdef NETATALK
272 1.1 hwr case AF_APPLETALK:
273 1.8 explorer etype = ETHERTYPE_ATALK;
274 1.1 hwr break;
275 1.1 hwr #endif
276 1.1 hwr #ifdef NS
277 1.1 hwr case AF_NS:
278 1.8 explorer etype = ETHERTYPE_NS;
279 1.1 hwr break;
280 1.1 hwr #endif
281 1.1 hwr default:
282 1.1 hwr IF_DROP(&ifp->if_snd);
283 1.1 hwr m_freem(m);
284 1.38 itojun error = EAFNOSUPPORT;
285 1.38 itojun goto end;
286 1.1 hwr }
287 1.8 explorer M_PREPEND(m, sizeof(struct greip), M_DONTWAIT);
288 1.1 hwr } else {
289 1.1 hwr IF_DROP(&ifp->if_snd);
290 1.1 hwr m_freem(m);
291 1.38 itojun error = EINVAL;
292 1.38 itojun goto end;
293 1.1 hwr }
294 1.1 hwr
295 1.38 itojun if (m == NULL) { /* impossible */
296 1.1 hwr IF_DROP(&ifp->if_snd);
297 1.38 itojun error = ENOBUFS;
298 1.38 itojun goto end;
299 1.1 hwr }
300 1.1 hwr
301 1.8 explorer gh = mtod(m, struct greip *);
302 1.8 explorer if (sc->g_proto == IPPROTO_GRE) {
303 1.1 hwr /* we don't have any GRE flags for now */
304 1.1 hwr
305 1.8 explorer memset((void *)&gh->gi_g, 0, sizeof(struct gre_h));
306 1.8 explorer gh->gi_ptype = htons(etype);
307 1.1 hwr }
308 1.1 hwr
309 1.1 hwr gh->gi_pr = sc->g_proto;
310 1.3 hwr if (sc->g_proto != IPPROTO_MOBILE) {
311 1.3 hwr gh->gi_src = sc->g_src;
312 1.3 hwr gh->gi_dst = sc->g_dst;
313 1.20 itojun ((struct ip*)gh)->ip_hl = (sizeof(struct ip)) >> 2;
314 1.26 martin ((struct ip*)gh)->ip_ttl = ip_gre_ttl;
315 1.41 itojun ((struct ip*)gh)->ip_tos = ip->ip_tos;
316 1.49 itojun gh->gi_len = htons(m->m_pkthdr.len);
317 1.3 hwr }
318 1.1 hwr
319 1.1 hwr ifp->if_opackets++;
320 1.8 explorer ifp->if_obytes += m->m_pkthdr.len;
321 1.1 hwr /* send it off */
322 1.46 jonathan error = ip_output(m, NULL, &sc->route, 0,
323 1.47 itojun (struct ip_moptions *)NULL, (struct socket *)NULL);
324 1.38 itojun end:
325 1.8 explorer if (error)
326 1.1 hwr ifp->if_oerrors++;
327 1.8 explorer return (error);
328 1.1 hwr }
329 1.1 hwr
330 1.1 hwr int
331 1.8 explorer gre_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
332 1.1 hwr {
333 1.13 martin struct proc *p = curproc; /* XXX */
334 1.8 explorer struct ifreq *ifr = (struct ifreq *)data;
335 1.28 itojun struct if_laddrreq *lifr = (struct if_laddrreq *)data;
336 1.8 explorer struct gre_softc *sc = ifp->if_softc;
337 1.1 hwr int s;
338 1.1 hwr struct sockaddr_in si;
339 1.8 explorer struct sockaddr *sa = NULL;
340 1.1 hwr int error;
341 1.1 hwr
342 1.8 explorer error = 0;
343 1.1 hwr
344 1.18 thorpej s = splnet();
345 1.20 itojun switch (cmd) {
346 1.20 itojun case SIOCSIFADDR:
347 1.36 itojun ifp->if_flags |= IFF_UP;
348 1.36 itojun break;
349 1.55 perry case SIOCSIFDSTADDR:
350 1.1 hwr break;
351 1.1 hwr case SIOCSIFFLAGS:
352 1.13 martin if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
353 1.13 martin break;
354 1.35 itojun if ((ifr->ifr_flags & IFF_LINK0) != 0)
355 1.35 itojun sc->g_proto = IPPROTO_GRE;
356 1.35 itojun else
357 1.35 itojun sc->g_proto = IPPROTO_MOBILE;
358 1.1 hwr break;
359 1.20 itojun case SIOCSIFMTU:
360 1.13 martin if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
361 1.13 martin break;
362 1.27 martin if (ifr->ifr_mtu < 576) {
363 1.1 hwr error = EINVAL;
364 1.1 hwr break;
365 1.1 hwr }
366 1.1 hwr ifp->if_mtu = ifr->ifr_mtu;
367 1.1 hwr break;
368 1.1 hwr case SIOCGIFMTU:
369 1.1 hwr ifr->ifr_mtu = sc->sc_if.if_mtu;
370 1.1 hwr break;
371 1.1 hwr case SIOCADDMULTI:
372 1.1 hwr case SIOCDELMULTI:
373 1.13 martin if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
374 1.13 martin break;
375 1.8 explorer if (ifr == 0) {
376 1.1 hwr error = EAFNOSUPPORT;
377 1.1 hwr break;
378 1.1 hwr }
379 1.8 explorer switch (ifr->ifr_addr.sa_family) {
380 1.1 hwr #ifdef INET
381 1.1 hwr case AF_INET:
382 1.1 hwr break;
383 1.1 hwr #endif
384 1.1 hwr default:
385 1.1 hwr error = EAFNOSUPPORT;
386 1.1 hwr break;
387 1.1 hwr }
388 1.1 hwr break;
389 1.1 hwr case GRESPROTO:
390 1.13 martin if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
391 1.13 martin break;
392 1.1 hwr sc->g_proto = ifr->ifr_flags;
393 1.1 hwr switch (sc->g_proto) {
394 1.40 itojun case IPPROTO_GRE:
395 1.3 hwr ifp->if_flags |= IFF_LINK0;
396 1.1 hwr break;
397 1.40 itojun case IPPROTO_MOBILE:
398 1.35 itojun ifp->if_flags &= ~IFF_LINK0;
399 1.1 hwr break;
400 1.1 hwr default:
401 1.40 itojun error = EPROTONOSUPPORT;
402 1.35 itojun break;
403 1.1 hwr }
404 1.1 hwr break;
405 1.1 hwr case GREGPROTO:
406 1.1 hwr ifr->ifr_flags = sc->g_proto;
407 1.1 hwr break;
408 1.1 hwr case GRESADDRS:
409 1.1 hwr case GRESADDRD:
410 1.13 martin if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
411 1.13 martin break;
412 1.1 hwr /*
413 1.20 itojun * set tunnel endpoints, compute a less specific route
414 1.20 itojun * to the remote end and mark if as up
415 1.20 itojun */
416 1.1 hwr sa = &ifr->ifr_addr;
417 1.30 itojun if (cmd == GRESADDRS)
418 1.1 hwr sc->g_src = (satosin(sa))->sin_addr;
419 1.30 itojun if (cmd == GRESADDRD)
420 1.1 hwr sc->g_dst = (satosin(sa))->sin_addr;
421 1.30 itojun recompute:
422 1.1 hwr if ((sc->g_src.s_addr != INADDR_ANY) &&
423 1.1 hwr (sc->g_dst.s_addr != INADDR_ANY)) {
424 1.1 hwr if (sc->route.ro_rt != 0) /* free old route */
425 1.1 hwr RTFREE(sc->route.ro_rt);
426 1.25 martin if (gre_compute_route(sc) == 0)
427 1.39 itojun ifp->if_flags |= IFF_RUNNING;
428 1.39 itojun else
429 1.39 itojun ifp->if_flags &= ~IFF_RUNNING;
430 1.1 hwr }
431 1.1 hwr break;
432 1.1 hwr case GREGADDRS:
433 1.33 itojun memset(&si, 0, sizeof(si));
434 1.28 itojun si.sin_family = AF_INET;
435 1.28 itojun si.sin_len = sizeof(struct sockaddr_in);
436 1.1 hwr si.sin_addr.s_addr = sc->g_src.s_addr;
437 1.8 explorer sa = sintosa(&si);
438 1.1 hwr ifr->ifr_addr = *sa;
439 1.1 hwr break;
440 1.1 hwr case GREGADDRD:
441 1.33 itojun memset(&si, 0, sizeof(si));
442 1.28 itojun si.sin_family = AF_INET;
443 1.28 itojun si.sin_len = sizeof(struct sockaddr_in);
444 1.1 hwr si.sin_addr.s_addr = sc->g_dst.s_addr;
445 1.8 explorer sa = sintosa(&si);
446 1.1 hwr ifr->ifr_addr = *sa;
447 1.28 itojun break;
448 1.28 itojun case SIOCSLIFPHYADDR:
449 1.28 itojun if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
450 1.28 itojun break;
451 1.28 itojun if (lifr->addr.ss_family != AF_INET ||
452 1.31 itojun lifr->dstaddr.ss_family != AF_INET) {
453 1.31 itojun error = EAFNOSUPPORT;
454 1.31 itojun break;
455 1.31 itojun }
456 1.28 itojun if (lifr->addr.ss_len != sizeof(si) ||
457 1.31 itojun lifr->dstaddr.ss_len != sizeof(si)) {
458 1.31 itojun error = EINVAL;
459 1.31 itojun break;
460 1.31 itojun }
461 1.28 itojun sc->g_src = (satosin((struct sockadrr *)&lifr->addr))->sin_addr;
462 1.28 itojun sc->g_dst =
463 1.28 itojun (satosin((struct sockadrr *)&lifr->dstaddr))->sin_addr;
464 1.30 itojun goto recompute;
465 1.28 itojun case SIOCDIFPHYADDR:
466 1.28 itojun if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
467 1.28 itojun break;
468 1.28 itojun sc->g_src.s_addr = INADDR_ANY;
469 1.28 itojun sc->g_dst.s_addr = INADDR_ANY;
470 1.28 itojun break;
471 1.28 itojun case SIOCGLIFPHYADDR:
472 1.28 itojun if (sc->g_src.s_addr == INADDR_ANY ||
473 1.28 itojun sc->g_dst.s_addr == INADDR_ANY) {
474 1.28 itojun error = EADDRNOTAVAIL;
475 1.28 itojun break;
476 1.28 itojun }
477 1.33 itojun memset(&si, 0, sizeof(si));
478 1.28 itojun si.sin_family = AF_INET;
479 1.28 itojun si.sin_len = sizeof(struct sockaddr_in);
480 1.28 itojun si.sin_addr.s_addr = sc->g_src.s_addr;
481 1.33 itojun memcpy(&lifr->addr, &si, sizeof(si));
482 1.28 itojun si.sin_addr.s_addr = sc->g_dst.s_addr;
483 1.33 itojun memcpy(&lifr->dstaddr, &si, sizeof(si));
484 1.1 hwr break;
485 1.1 hwr default:
486 1.1 hwr error = EINVAL;
487 1.31 itojun break;
488 1.1 hwr }
489 1.1 hwr
490 1.1 hwr splx(s);
491 1.8 explorer return (error);
492 1.1 hwr }
493 1.1 hwr
494 1.20 itojun /*
495 1.1 hwr * computes a route to our destination that is not the one
496 1.1 hwr * which would be taken by ip_output(), as this one will loop back to
497 1.1 hwr * us. If the interface is p2p as a--->b, then a routing entry exists
498 1.1 hwr * If we now send a packet to b (e.g. ping b), this will come down here
499 1.43 wiz * gets src=a, dst=b tacked on and would from ip_output() sent back to
500 1.1 hwr * if_gre.
501 1.1 hwr * Goal here is to compute a route to b that is less specific than
502 1.1 hwr * a-->b. We know that this one exists as in normal operation we have
503 1.1 hwr * at least a default route which matches.
504 1.1 hwr */
505 1.25 martin int
506 1.8 explorer gre_compute_route(struct gre_softc *sc)
507 1.1 hwr {
508 1.1 hwr struct route *ro;
509 1.8 explorer u_int32_t a, b, c;
510 1.1 hwr
511 1.8 explorer ro = &sc->route;
512 1.20 itojun
513 1.8 explorer memset(ro, 0, sizeof(struct route));
514 1.8 explorer ((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst;
515 1.8 explorer ro->ro_dst.sa_family = AF_INET;
516 1.8 explorer ro->ro_dst.sa_len = sizeof(ro->ro_dst);
517 1.8 explorer
518 1.1 hwr /*
519 1.1 hwr * toggle last bit, so our interface is not found, but a less
520 1.20 itojun * specific route. I'd rather like to specify a shorter mask,
521 1.20 itojun * but this is not possible. Should work though. XXX
522 1.1 hwr * there is a simpler way ...
523 1.20 itojun */
524 1.8 explorer if ((sc->sc_if.if_flags & IFF_LINK1) == 0) {
525 1.8 explorer a = ntohl(sc->g_dst.s_addr);
526 1.8 explorer b = a & 0x01;
527 1.8 explorer c = a & 0xfffffffe;
528 1.8 explorer b = b ^ 0x01;
529 1.8 explorer a = b | c;
530 1.8 explorer ((struct sockaddr_in *)&ro->ro_dst)->sin_addr.s_addr
531 1.20 itojun = htonl(a);
532 1.8 explorer }
533 1.1 hwr
534 1.1 hwr #ifdef DIAGNOSTIC
535 1.45 grant printf("%s: searching for a route to %s", sc->sc_if.if_xname,
536 1.19 itojun inet_ntoa(((struct sockaddr_in *)&ro->ro_dst)->sin_addr));
537 1.1 hwr #endif
538 1.1 hwr
539 1.1 hwr rtalloc(ro);
540 1.1 hwr
541 1.1 hwr /*
542 1.25 martin * check if this returned a route at all and this route is no
543 1.25 martin * recursion to ourself
544 1.25 martin */
545 1.25 martin if (ro->ro_rt == NULL || ro->ro_rt->rt_ifp->if_softc == sc) {
546 1.25 martin #ifdef DIAGNOSTIC
547 1.25 martin if (ro->ro_rt == NULL)
548 1.25 martin printf(" - no route found!\n");
549 1.25 martin else
550 1.25 martin printf(" - route loops back to ourself!\n");
551 1.25 martin #endif
552 1.25 martin return EADDRNOTAVAIL;
553 1.25 martin }
554 1.25 martin
555 1.25 martin /*
556 1.20 itojun * now change it back - else ip_output will just drop
557 1.20 itojun * the route and search one to this interface ...
558 1.20 itojun */
559 1.8 explorer if ((sc->sc_if.if_flags & IFF_LINK1) == 0)
560 1.8 explorer ((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst;
561 1.1 hwr
562 1.1 hwr #ifdef DIAGNOSTIC
563 1.19 itojun printf(", choosing %s with gateway %s", ro->ro_rt->rt_ifp->if_xname,
564 1.19 itojun inet_ntoa(((struct sockaddr_in *)(ro->ro_rt->rt_gateway))->sin_addr));
565 1.8 explorer printf("\n");
566 1.1 hwr #endif
567 1.25 martin
568 1.25 martin return 0;
569 1.1 hwr }
570 1.1 hwr
571 1.3 hwr /*
572 1.20 itojun * do a checksum of a buffer - much like in_cksum, which operates on
573 1.20 itojun * mbufs.
574 1.3 hwr */
575 1.48 itojun u_int16_t
576 1.48 itojun gre_in_cksum(u_int16_t *p, u_int len)
577 1.3 hwr {
578 1.48 itojun u_int32_t sum = 0;
579 1.3 hwr int nwords = len >> 1;
580 1.20 itojun
581 1.3 hwr while (nwords-- != 0)
582 1.3 hwr sum += *p++;
583 1.20 itojun
584 1.20 itojun if (len & 1) {
585 1.20 itojun union {
586 1.20 itojun u_short w;
587 1.20 itojun u_char c[2];
588 1.20 itojun } u;
589 1.20 itojun u.c[0] = *(u_char *)p;
590 1.20 itojun u.c[1] = 0;
591 1.20 itojun sum += u.w;
592 1.20 itojun }
593 1.20 itojun
594 1.20 itojun /* end-around-carry */
595 1.20 itojun sum = (sum >> 16) + (sum & 0xffff);
596 1.20 itojun sum += (sum >> 16);
597 1.20 itojun return (~sum);
598 1.3 hwr }
599 1.54 christos #endif
600 1.54 christos
601 1.54 christos void greattach __P((int));
602 1.54 christos
603 1.54 christos /* ARGSUSED */
604 1.54 christos void
605 1.54 christos greattach(count)
606 1.54 christos int count;
607 1.54 christos {
608 1.54 christos #ifdef INET
609 1.54 christos LIST_INIT(&gre_softc_list);
610 1.54 christos if_clone_attach(&gre_cloner);
611 1.54 christos #endif
612 1.54 christos }
613