1 /* 2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the project nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * $FreeBSD$ 30 */ 31 32 /* 33 * gif.c 34 */ 35 36 #include "opt_inet.h" 37 #include "opt_inet6.h" 38 39 #include <sys/param.h> 40 #include <sys/systm.h> 41 #include <sys/kernel.h> 42 #include <sys/malloc.h> 43 #include <sys/mbuf.h> 44 #include <sys/socket.h> 45 #include <sys/sockio.h> 46 #include <sys/errno.h> 47 #include <sys/time.h> 48 #include <sys/syslog.h> 49 #include <machine/cpu.h> 50 51 #include <net/if.h> 52 #include <net/if_types.h> 53 #include <net/netisr.h> 54 #include <net/route.h> 55 #include <net/bpf.h> 56 57 #ifdef INET 58 #include <netinet/in.h> 59 #include <netinet/in_systm.h> 60 #include <netinet/in_var.h> 61 #include <netinet/ip.h> 62 #include <netinet/in_gif.h> 63 #endif /* INET */ 64 65 #ifdef INET6 66 #ifndef INET 67 #include <netinet/in.h> 68 #endif 69 #include <netinet6/in6_var.h> 70 #include <netinet/ip6.h> 71 #include <netinet6/ip6_var.h> 72 #include <netinet6/in6_gif.h> 73 #endif /* INET6 */ 74 75 #include <net/if_gif.h> 76 77 #include "gif.h" 78 79 #include <net/net_osdep.h> 80 81 #if NGIF > 0 82 83 void gifattach __P((void *)); 84 85 /* 86 * gif global variable definitions 87 */ 88 int ngif = NGIF; /* number of interfaces */ 89 struct gif_softc *gif = 0; 90 91 void 92 gifattach(dummy) 93 void *dummy; 94 { 95 register struct gif_softc *sc; 96 register int i; 97 98 gif = sc = malloc (ngif * sizeof(struct gif_softc), M_DEVBUF, M_WAIT); 99 bzero(sc, ngif * sizeof(struct gif_softc)); 100 for (i = 0; i < ngif; sc++, i++) { 101 sc->gif_if.if_name = "gif"; 102 sc->gif_if.if_unit = i; 103 sc->gif_if.if_mtu = GIF_MTU; 104 sc->gif_if.if_flags = IFF_POINTOPOINT | IFF_MULTICAST; 105 sc->gif_if.if_ioctl = gif_ioctl; 106 sc->gif_if.if_output = gif_output; 107 sc->gif_if.if_type = IFT_GIF; 108 sc->gif_if.if_snd.ifq_maxlen = ifqmaxlen; 109 if_attach(&sc->gif_if); 110 bpfattach(&sc->gif_if, DLT_NULL, sizeof(u_int)); 111 } 112 } 113 114 PSEUDO_SET(gifattach, if_gif); 115 116 int 117 gif_output(ifp, m, dst, rt) 118 struct ifnet *ifp; 119 struct mbuf *m; 120 struct sockaddr *dst; 121 struct rtentry *rt; /* added in net2 */ 122 { 123 register struct gif_softc *sc = (struct gif_softc*)ifp; 124 int error = 0; 125 static int called = 0; /* XXX: MUTEX */ 126 int calllimit = 10; /* XXX: adhoc */ 127 128 /* 129 * gif may cause infinite recursion calls when misconfigured. 130 * We'll prevent this by introducing upper limit. 131 * XXX: this mechanism may introduce another problem about 132 * mutual exclusion of the variable CALLED, especially if we 133 * use kernel thread. 134 */ 135 if (++called >= calllimit) { 136 log(LOG_NOTICE, 137 "gif_output: recursively called too many times(%d)\n", 138 called); 139 m_freem(m); 140 error = EIO; /* is there better errno? */ 141 goto end; 142 } 143 getmicrotime(&ifp->if_lastchange); 144 m->m_flags &= ~(M_BCAST|M_MCAST); 145 if (!(ifp->if_flags & IFF_UP) || 146 sc->gif_psrc == NULL || sc->gif_pdst == NULL) { 147 m_freem(m); 148 error = ENETDOWN; 149 goto end; 150 } 151 152 if (ifp->if_bpf) { 153 /* 154 * We need to prepend the address family as 155 * a four byte field. Cons up a dummy header 156 * to pacify bpf. This is safe because bpf 157 * will only read from the mbuf (i.e., it won't 158 * try to free it or keep a pointer a to it). 159 */ 160 struct mbuf m0; 161 u_int af = dst->sa_family; 162 163 m0.m_next = m; 164 m0.m_len = 4; 165 m0.m_data = (char *)⁡ 166 167 bpf_mtap(ifp, &m0); 168 } 169 ifp->if_opackets++; 170 ifp->if_obytes += m->m_pkthdr.len; 171 172 switch (sc->gif_psrc->sa_family) { 173 #ifdef INET 174 case AF_INET: 175 error = in_gif_output(ifp, dst->sa_family, m, rt); 176 break; 177 #endif 178 #ifdef INET6 179 case AF_INET6: 180 error = in6_gif_output(ifp, dst->sa_family, m, rt); 181 break; 182 #endif 183 default: 184 m_freem(m); 185 error = ENETDOWN; 186 } 187 188 end: 189 called = 0; /* reset recursion counter */ 190 if (error) ifp->if_oerrors++; 191 return error; 192 } 193 194 void 195 gif_input(m, af, gifp) 196 struct mbuf *m; 197 int af; 198 struct ifnet *gifp; 199 { 200 int s, isr; 201 register struct ifqueue *ifq = 0; 202 203 if (gifp == NULL) { 204 /* just in case */ 205 m_freem(m); 206 return; 207 } 208 209 if (m->m_pkthdr.rcvif) 210 m->m_pkthdr.rcvif = gifp; 211 212 if (gifp->if_bpf) { 213 /* 214 * We need to prepend the address family as 215 * a four byte field. Cons up a dummy header 216 * to pacify bpf. This is safe because bpf 217 * will only read from the mbuf (i.e., it won't 218 * try to free it or keep a pointer a to it). 219 */ 220 struct mbuf m0; 221 u_int af = AF_INET6; 222 223 m0.m_next = m; 224 m0.m_len = 4; 225 m0.m_data = (char *)⁡ 226 227 bpf_mtap(gifp, &m0); 228 } 229 230 /* 231 * Put the packet to the network layer input queue according to the 232 * specified address family. 233 * Note: older versions of gif_input directly called network layer 234 * input functions, e.g. ip6_input, here. We changed the policy to 235 * prevent too many recursive calls of such input functions, which 236 * might cause kernel panic. But the change may introduce another 237 * problem; if the input queue is full, packets are discarded. 238 * We believed it rarely occurs and changed the policy. If we find 239 * it occurs more times than we thought, we may change the policy 240 * again. 241 */ 242 switch (af) { 243 #ifdef INET 244 case AF_INET: 245 ifq = &ipintrq; 246 isr = NETISR_IP; 247 break; 248 #endif 249 #ifdef INET6 250 case AF_INET6: 251 ifq = &ip6intrq; 252 isr = NETISR_IPV6; 253 break; 254 #endif 255 default: 256 m_freem(m); 257 return; 258 } 259 260 s = splimp(); 261 if (IF_QFULL(ifq)) { 262 IF_DROP(ifq); /* update statistics */ 263 m_freem(m); 264 splx(s); 265 return; 266 } 267 IF_ENQUEUE(ifq, m); 268 /* we need schednetisr since the address family may change */ 269 schednetisr(isr); 270 gifp->if_ipackets++; 271 gifp->if_ibytes += m->m_pkthdr.len; 272 splx(s); 273 274 return; 275 } 276 277 278 int 279 gif_ioctl(ifp, cmd, data) 280 struct ifnet *ifp; 281 u_long cmd; 282 caddr_t data; 283 { 284 struct gif_softc *sc = (struct gif_softc*)ifp; 285 struct ifreq *ifr = (struct ifreq*)data; 286 int error = 0, size; 287 struct sockaddr *sa, *dst, *src; 288 289 switch (cmd) { 290 case SIOCSIFADDR: 291 break; 292 293 case SIOCSIFDSTADDR: 294 break; 295 296 case SIOCADDMULTI: 297 case SIOCDELMULTI: 298 break; 299 300 case SIOCGIFMTU: 301 break; 302 case SIOCSIFMTU: 303 { 304 u_long mtu; 305 mtu = ifr->ifr_mtu; 306 if (mtu < GIF_MTU_MIN || mtu > GIF_MTU_MAX) { 307 return (EINVAL); 308 } 309 ifp->if_mtu = mtu; 310 } 311 break; 312 313 case SIOCSIFPHYADDR: 314 #ifdef INET6 315 case SIOCSIFPHYADDR_IN6: 316 #endif /* INET6 */ 317 switch (ifr->ifr_addr.sa_family) { 318 #ifdef INET 319 case AF_INET: 320 src = (struct sockaddr *) 321 &(((struct in_aliasreq *)data)->ifra_addr); 322 dst = (struct sockaddr *) 323 &(((struct in_aliasreq *)data)->ifra_dstaddr); 324 325 /* only one gif can have dst = INADDR_ANY */ 326 #define satosaddr(sa) (((struct sockaddr_in *)(sa))->sin_addr.s_addr) 327 328 if (satosaddr(dst) == INADDR_ANY) { 329 int i; 330 struct gif_softc *sc2; 331 332 for (i = 0, sc2 = gif; i < ngif; i++, sc2++) { 333 if (sc2 == sc) continue; 334 if (sc2->gif_pdst && 335 satosaddr(sc2->gif_pdst) 336 == INADDR_ANY) { 337 error = EADDRNOTAVAIL; 338 goto bad; 339 } 340 } 341 } 342 size = sizeof(struct sockaddr_in); 343 break; 344 #endif /* INET */ 345 #ifdef INET6 346 case AF_INET6: 347 src = (struct sockaddr *) 348 &(((struct in6_aliasreq *)data)->ifra_addr); 349 dst = (struct sockaddr *) 350 &(((struct in6_aliasreq *)data)->ifra_dstaddr); 351 352 /* only one gif can have dst = in6addr_any */ 353 #define satoin6(sa) (&((struct sockaddr_in6 *)(sa))->sin6_addr) 354 355 if (IN6_IS_ADDR_UNSPECIFIED(satoin6(dst))) { 356 int i; 357 struct gif_softc *sc2; 358 359 for (i = 0, sc2 = gif; i < ngif; i++, sc2++) { 360 if (sc2 == sc) continue; 361 if (sc2->gif_pdst && 362 IN6_IS_ADDR_UNSPECIFIED( 363 satoin6(sc2->gif_pdst) 364 )) { 365 error = EADDRNOTAVAIL; 366 goto bad; 367 } 368 } 369 } 370 size = sizeof(struct sockaddr_in6); 371 break; 372 #endif /* INET6 */ 373 default: 374 error = EPROTOTYPE; 375 goto bad; 376 break; 377 } 378 if (sc->gif_psrc != NULL) 379 free((caddr_t)sc->gif_psrc, M_IFADDR); 380 if (sc->gif_pdst != NULL) 381 free((caddr_t)sc->gif_pdst, M_IFADDR); 382 383 sa = (struct sockaddr *)malloc(size, M_IFADDR, M_WAITOK); 384 bzero((caddr_t)sa, size); 385 bcopy((caddr_t)src, (caddr_t)sa, size); 386 sc->gif_psrc = sa; 387 388 sa = (struct sockaddr *)malloc(size, M_IFADDR, M_WAITOK); 389 bzero((caddr_t)sa, size); 390 bcopy((caddr_t)dst, (caddr_t)sa, size); 391 sc->gif_pdst = sa; 392 393 ifp->if_flags |= (IFF_UP|IFF_RUNNING); 394 if_up(ifp); /* send up RTM_IFINFO */ 395 396 break; 397 398 case SIOCGIFPSRCADDR: 399 #ifdef INET6 400 case SIOCGIFPSRCADDR_IN6: 401 #endif /* INET6 */ 402 if (sc->gif_psrc == NULL) { 403 error = EADDRNOTAVAIL; 404 goto bad; 405 } 406 src = sc->gif_psrc; 407 switch (sc->gif_psrc->sa_family) { 408 #ifdef INET 409 case AF_INET: 410 dst = &ifr->ifr_addr; 411 size = sizeof(struct sockaddr_in); 412 break; 413 #endif /* INET */ 414 #ifdef INET6 415 case AF_INET6: 416 dst = (struct sockaddr *) 417 &(((struct in6_ifreq *)data)->ifr_addr); 418 size = sizeof(struct sockaddr_in6); 419 break; 420 #endif /* INET6 */ 421 default: 422 error = EADDRNOTAVAIL; 423 goto bad; 424 } 425 bcopy((caddr_t)src, (caddr_t)dst, size); 426 break; 427 428 case SIOCGIFPDSTADDR: 429 #ifdef INET6 430 case SIOCGIFPDSTADDR_IN6: 431 #endif /* INET6 */ 432 if (sc->gif_pdst == NULL) { 433 error = EADDRNOTAVAIL; 434 goto bad; 435 } 436 src = sc->gif_pdst; 437 switch (sc->gif_pdst->sa_family) { 438 #ifdef INET 439 case AF_INET: 440 dst = &ifr->ifr_addr; 441 size = sizeof(struct sockaddr_in); 442 break; 443 #endif /* INET */ 444 #ifdef INET6 445 case AF_INET6: 446 dst = (struct sockaddr *) 447 &(((struct in6_ifreq *)data)->ifr_addr); 448 size = sizeof(struct sockaddr_in6); 449 break; 450 #endif /* INET6 */ 451 default: 452 error = EADDRNOTAVAIL; 453 goto bad; 454 } 455 bcopy((caddr_t)src, (caddr_t)dst, size); 456 break; 457 458 case SIOCSIFFLAGS: 459 break; 460 461 default: 462 error = EINVAL; 463 break; 464 } 465 bad: 466 return error; 467 } 468 #endif /*NGIF > 0*/ 469