[39e6e65a] | 1 | /* |
---|
| 2 | * Copyright (c) 1982, 1986, 1988, 1990, 1993 |
---|
| 3 | * The Regents of the University of California. 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 | * 4. Neither the name of the University 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 REGENTS 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 REGENTS 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 | * @(#)ip_output.c 8.3 (Berkeley) 1/21/94 |
---|
[0c67b436] | 30 | * $FreeBSD: src/sys/netinet/ip_output.c,v 1.271 2007/03/23 09:43:36 bms Exp $ |
---|
| 31 | */ |
---|
| 32 | |
---|
| 33 | /* |
---|
[39e6e65a] | 34 | * $Id$ |
---|
| 35 | */ |
---|
| 36 | |
---|
| 37 | #define _IP_VHL |
---|
| 38 | |
---|
| 39 | #include <sys/param.h> |
---|
[c301570] | 40 | #include <rtems/bsd/sys/queue.h> |
---|
[39e6e65a] | 41 | #include <sys/systm.h> |
---|
| 42 | #include <sys/malloc.h> |
---|
| 43 | #include <sys/mbuf.h> |
---|
[b3f8c9e1] | 44 | #include <errno.h> |
---|
[39e6e65a] | 45 | #include <sys/protosw.h> |
---|
| 46 | #include <sys/socket.h> |
---|
| 47 | #include <sys/socketvar.h> |
---|
| 48 | |
---|
| 49 | #include <net/if.h> |
---|
| 50 | #include <net/route.h> |
---|
| 51 | |
---|
| 52 | #include <netinet/in.h> |
---|
| 53 | #include <netinet/in_systm.h> |
---|
| 54 | #include <netinet/ip.h> |
---|
| 55 | #include <netinet/in_pcb.h> |
---|
| 56 | #include <netinet/in_var.h> |
---|
| 57 | #include <netinet/ip_var.h> |
---|
| 58 | |
---|
| 59 | #include <machine/in_cksum.h> |
---|
| 60 | |
---|
| 61 | #if !defined(COMPAT_IPFW) || COMPAT_IPFW == 1 |
---|
| 62 | #undef COMPAT_IPFW |
---|
| 63 | #define COMPAT_IPFW 1 |
---|
| 64 | #else |
---|
| 65 | #undef COMPAT_IPFW |
---|
| 66 | #endif |
---|
| 67 | |
---|
| 68 | u_short ip_id; |
---|
| 69 | |
---|
[e5d6315e] | 70 | static struct mbuf *ip_insertoptions(struct mbuf *, struct mbuf *, int *); |
---|
[39e6e65a] | 71 | static void ip_mloopback |
---|
[e5d6315e] | 72 | (struct ifnet *, struct mbuf *, struct sockaddr_in *, int); |
---|
[39e6e65a] | 73 | static int ip_getmoptions |
---|
[e5d6315e] | 74 | (int, struct ip_moptions *, struct mbuf **); |
---|
| 75 | static int ip_optcopy(struct ip *, struct ip *); |
---|
| 76 | static int ip_pcbopts(struct mbuf **, struct mbuf *); |
---|
[39e6e65a] | 77 | static int ip_setmoptions |
---|
[e5d6315e] | 78 | (int, struct ip_moptions **, struct mbuf *); |
---|
[39e6e65a] | 79 | |
---|
| 80 | extern struct protosw inetsw[]; |
---|
| 81 | |
---|
| 82 | /* |
---|
| 83 | * IP output. The packet in mbuf chain m contains a skeletal IP |
---|
| 84 | * header (with len, off, ttl, proto, tos, src, dst). |
---|
| 85 | * The mbuf chain containing the packet will be freed. |
---|
| 86 | * The mbuf opt, if present, will not be freed. |
---|
| 87 | */ |
---|
| 88 | int |
---|
[dd967330] | 89 | ip_output(struct mbuf *m0, struct mbuf *opt, struct route *ro, int flags, |
---|
| 90 | struct ip_moptions *imo) |
---|
[39e6e65a] | 91 | { |
---|
| 92 | struct ip *ip, *mhip; |
---|
| 93 | struct ifnet *ifp; |
---|
| 94 | struct mbuf *m = m0; |
---|
| 95 | int hlen = sizeof (struct ip); |
---|
[3274c876] | 96 | int len = 0, off, error = 0; |
---|
[39e6e65a] | 97 | struct sockaddr_in *dst; |
---|
| 98 | struct in_ifaddr *ia; |
---|
| 99 | int isbroadcast; |
---|
| 100 | |
---|
| 101 | #ifdef DIAGNOSTIC |
---|
| 102 | if ((m->m_flags & M_PKTHDR) == 0) |
---|
| 103 | panic("ip_output no HDR"); |
---|
| 104 | if (!ro) |
---|
| 105 | panic("ip_output no route, proto = %d", |
---|
| 106 | mtod(m, struct ip *)->ip_p); |
---|
| 107 | #endif |
---|
| 108 | if (opt) { |
---|
| 109 | m = ip_insertoptions(m, opt, &len); |
---|
| 110 | hlen = len; |
---|
| 111 | } |
---|
| 112 | ip = mtod(m, struct ip *); |
---|
| 113 | /* |
---|
| 114 | * Fill in IP header. |
---|
| 115 | */ |
---|
| 116 | if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) { |
---|
[f94e799e] | 117 | #ifdef _IP_VHL |
---|
[39e6e65a] | 118 | ip->ip_vhl = IP_MAKE_VHL(IPVERSION, hlen >> 2); |
---|
[f94e799e] | 119 | #else |
---|
| 120 | ip->ip_v = IPVERSION; |
---|
| 121 | ip->ip_hl = hlen >> 2; |
---|
| 122 | #endif |
---|
[39e6e65a] | 123 | ip->ip_off &= IP_DF; |
---|
| 124 | ip->ip_id = htons(ip_id++); |
---|
| 125 | ipstat.ips_localout++; |
---|
| 126 | } else { |
---|
[491f63a] | 127 | #ifdef _IP_VHL |
---|
[39e6e65a] | 128 | hlen = IP_VHL_HL(ip->ip_vhl) << 2; |
---|
[491f63a] | 129 | #else |
---|
| 130 | hlen = ip->ip_hl << 2; |
---|
| 131 | #endif |
---|
[39e6e65a] | 132 | } |
---|
| 133 | |
---|
| 134 | dst = (struct sockaddr_in *)&ro->ro_dst; |
---|
| 135 | /* |
---|
| 136 | * If there is a cached route, |
---|
| 137 | * check that it is to the same destination |
---|
| 138 | * and is still up. If not, free it and try again. |
---|
| 139 | */ |
---|
| 140 | if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 || |
---|
| 141 | dst->sin_addr.s_addr != ip->ip_dst.s_addr)) { |
---|
| 142 | RTFREE(ro->ro_rt); |
---|
| 143 | ro->ro_rt = (struct rtentry *)0; |
---|
| 144 | } |
---|
[0c67b436] | 145 | if (ro->ro_rt == NULL) { |
---|
[39e6e65a] | 146 | dst->sin_family = AF_INET; |
---|
| 147 | dst->sin_len = sizeof(*dst); |
---|
| 148 | dst->sin_addr = ip->ip_dst; |
---|
| 149 | } |
---|
| 150 | /* |
---|
| 151 | * If routing to interface only, |
---|
| 152 | * short circuit routing lookup. |
---|
| 153 | */ |
---|
| 154 | #define ifatoia(ifa) ((struct in_ifaddr *)(ifa)) |
---|
| 155 | #define sintosa(sin) ((struct sockaddr *)(sin)) |
---|
| 156 | if (flags & IP_ROUTETOIF) { |
---|
| 157 | if ((ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst)))) == 0 && |
---|
| 158 | (ia = ifatoia(ifa_ifwithnet(sintosa(dst)))) == 0) { |
---|
| 159 | ipstat.ips_noroute++; |
---|
| 160 | error = ENETUNREACH; |
---|
| 161 | goto bad; |
---|
| 162 | } |
---|
| 163 | ifp = ia->ia_ifp; |
---|
| 164 | ip->ip_ttl = 1; |
---|
| 165 | isbroadcast = in_broadcast(dst->sin_addr, ifp); |
---|
| 166 | } else { |
---|
| 167 | /* |
---|
| 168 | * If this is the case, we probably don't want to allocate |
---|
| 169 | * a protocol-cloned route since we didn't get one from the |
---|
| 170 | * ULP. This lets TCP do its thing, while not burdening |
---|
| 171 | * forwarding or ICMP with the overhead of cloning a route. |
---|
| 172 | * Of course, we still want to do any cloning requested by |
---|
| 173 | * the link layer, as this is probably required in all cases |
---|
| 174 | * for correct operation (as it is for ARP). |
---|
| 175 | */ |
---|
| 176 | if (ro->ro_rt == 0) |
---|
| 177 | rtalloc_ign(ro, RTF_PRCLONING); |
---|
| 178 | if (ro->ro_rt == 0) { |
---|
| 179 | ipstat.ips_noroute++; |
---|
| 180 | error = EHOSTUNREACH; |
---|
| 181 | goto bad; |
---|
| 182 | } |
---|
| 183 | ia = ifatoia(ro->ro_rt->rt_ifa); |
---|
| 184 | ifp = ro->ro_rt->rt_ifp; |
---|
| 185 | ro->ro_rt->rt_use++; |
---|
| 186 | if (ro->ro_rt->rt_flags & RTF_GATEWAY) |
---|
| 187 | dst = (struct sockaddr_in *)ro->ro_rt->rt_gateway; |
---|
| 188 | if (ro->ro_rt->rt_flags & RTF_HOST) |
---|
| 189 | isbroadcast = (ro->ro_rt->rt_flags & RTF_BROADCAST); |
---|
| 190 | else |
---|
| 191 | isbroadcast = in_broadcast(dst->sin_addr, ifp); |
---|
| 192 | } |
---|
| 193 | if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) { |
---|
| 194 | struct in_multi *inm; |
---|
| 195 | |
---|
| 196 | m->m_flags |= M_MCAST; |
---|
| 197 | /* |
---|
| 198 | * IP destination address is multicast. Make sure "dst" |
---|
| 199 | * still points to the address in "ro". (It may have been |
---|
| 200 | * changed to point to a gateway address, above.) |
---|
| 201 | */ |
---|
| 202 | dst = (struct sockaddr_in *)&ro->ro_dst; |
---|
| 203 | /* |
---|
| 204 | * See if the caller provided any multicast options |
---|
| 205 | */ |
---|
| 206 | if (imo != NULL) { |
---|
| 207 | ip->ip_ttl = imo->imo_multicast_ttl; |
---|
| 208 | if (imo->imo_multicast_ifp != NULL) |
---|
| 209 | ifp = imo->imo_multicast_ifp; |
---|
| 210 | if (imo->imo_multicast_vif != -1) |
---|
| 211 | ip->ip_src.s_addr = |
---|
| 212 | ip_mcast_src(imo->imo_multicast_vif); |
---|
| 213 | } else |
---|
| 214 | ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL; |
---|
| 215 | /* |
---|
| 216 | * Confirm that the outgoing interface supports multicast. |
---|
| 217 | */ |
---|
| 218 | if ((imo == NULL) || (imo->imo_multicast_vif == -1)) { |
---|
| 219 | if ((ifp->if_flags & IFF_MULTICAST) == 0) { |
---|
| 220 | ipstat.ips_noroute++; |
---|
| 221 | error = ENETUNREACH; |
---|
| 222 | goto bad; |
---|
| 223 | } |
---|
| 224 | } |
---|
| 225 | /* |
---|
| 226 | * If source address not specified yet, use address |
---|
| 227 | * of outgoing interface. |
---|
| 228 | */ |
---|
| 229 | if (ip->ip_src.s_addr == INADDR_ANY) { |
---|
| 230 | register struct in_ifaddr *ia; |
---|
| 231 | |
---|
| 232 | for (ia = in_ifaddr; ia; ia = ia->ia_next) |
---|
| 233 | if (ia->ia_ifp == ifp) { |
---|
| 234 | ip->ip_src = IA_SIN(ia)->sin_addr; |
---|
| 235 | break; |
---|
| 236 | } |
---|
| 237 | } |
---|
| 238 | |
---|
| 239 | IN_LOOKUP_MULTI(ip->ip_dst, ifp, inm); |
---|
| 240 | if (inm != NULL && |
---|
| 241 | (imo == NULL || imo->imo_multicast_loop)) { |
---|
| 242 | /* |
---|
| 243 | * If we belong to the destination multicast group |
---|
| 244 | * on the outgoing interface, and the caller did not |
---|
| 245 | * forbid loopback, loop back a copy. |
---|
| 246 | */ |
---|
| 247 | ip_mloopback(ifp, m, dst, hlen); |
---|
| 248 | } |
---|
| 249 | else { |
---|
| 250 | /* |
---|
| 251 | * If we are acting as a multicast router, perform |
---|
| 252 | * multicast forwarding as if the packet had just |
---|
| 253 | * arrived on the interface to which we are about |
---|
| 254 | * to send. The multicast forwarding function |
---|
| 255 | * recursively calls this function, using the |
---|
| 256 | * IP_FORWARDING flag to prevent infinite recursion. |
---|
| 257 | * |
---|
| 258 | * Multicasts that are looped back by ip_mloopback(), |
---|
| 259 | * above, will be forwarded by the ip_input() routine, |
---|
| 260 | * if necessary. |
---|
| 261 | */ |
---|
| 262 | if (ip_mrouter && (flags & IP_FORWARDING) == 0) { |
---|
| 263 | /* |
---|
| 264 | * Check if rsvp daemon is running. If not, don't |
---|
| 265 | * set ip_moptions. This ensures that the packet |
---|
| 266 | * is multicast and not just sent down one link |
---|
| 267 | * as prescribed by rsvpd. |
---|
| 268 | */ |
---|
| 269 | if (!rsvp_on) |
---|
| 270 | imo = NULL; |
---|
| 271 | if (ip_mforward(ip, ifp, m, imo) != 0) { |
---|
| 272 | m_freem(m); |
---|
| 273 | goto done; |
---|
| 274 | } |
---|
| 275 | } |
---|
| 276 | } |
---|
| 277 | |
---|
| 278 | /* |
---|
| 279 | * Multicasts with a time-to-live of zero may be looped- |
---|
| 280 | * back, above, but must not be transmitted on a network. |
---|
| 281 | * Also, multicasts addressed to the loopback interface |
---|
| 282 | * are not sent -- the above call to ip_mloopback() will |
---|
| 283 | * loop back a copy if this host actually belongs to the |
---|
| 284 | * destination group on the loopback interface. |
---|
| 285 | */ |
---|
| 286 | if (ip->ip_ttl == 0 || ifp->if_flags & IFF_LOOPBACK) { |
---|
| 287 | m_freem(m); |
---|
| 288 | goto done; |
---|
| 289 | } |
---|
| 290 | |
---|
| 291 | goto sendit; |
---|
| 292 | } |
---|
| 293 | #ifndef notdef |
---|
| 294 | /* |
---|
| 295 | * If source address not specified yet, use address |
---|
| 296 | * of outgoing interface. |
---|
| 297 | */ |
---|
| 298 | if (ip->ip_src.s_addr == INADDR_ANY) |
---|
| 299 | ip->ip_src = IA_SIN(ia)->sin_addr; |
---|
| 300 | #endif |
---|
| 301 | /* |
---|
| 302 | * Verify that we have any chance at all of being able to queue |
---|
| 303 | * the packet or packet fragments |
---|
| 304 | */ |
---|
| 305 | if ((ifp->if_snd.ifq_len + ip->ip_len / ifp->if_mtu + 1) >= |
---|
| 306 | ifp->if_snd.ifq_maxlen) { |
---|
| 307 | error = ENOBUFS; |
---|
| 308 | goto bad; |
---|
| 309 | } |
---|
| 310 | |
---|
| 311 | /* |
---|
| 312 | * Look for broadcast address and |
---|
[0c67b436] | 313 | * verify user is allowed to send |
---|
[39e6e65a] | 314 | * such a packet. |
---|
| 315 | */ |
---|
| 316 | if (isbroadcast) { |
---|
| 317 | if ((ifp->if_flags & IFF_BROADCAST) == 0) { |
---|
| 318 | error = EADDRNOTAVAIL; |
---|
| 319 | goto bad; |
---|
| 320 | } |
---|
| 321 | if ((flags & IP_ALLOWBROADCAST) == 0) { |
---|
| 322 | error = EACCES; |
---|
| 323 | goto bad; |
---|
| 324 | } |
---|
| 325 | /* don't allow broadcast messages to be fragmented */ |
---|
[f94e799e] | 326 | if (ip->ip_len > ifp->if_mtu) { |
---|
[39e6e65a] | 327 | error = EMSGSIZE; |
---|
| 328 | goto bad; |
---|
| 329 | } |
---|
| 330 | m->m_flags |= M_BCAST; |
---|
| 331 | } else { |
---|
| 332 | m->m_flags &= ~M_BCAST; |
---|
| 333 | } |
---|
| 334 | |
---|
| 335 | sendit: |
---|
| 336 | /* |
---|
| 337 | * IpHack's section. |
---|
| 338 | * - Xlate: translate packet's addr/port (NAT). |
---|
| 339 | * - Firewall: deny/allow/etc. |
---|
| 340 | * - Wrap: fake packet's addr/port <unimpl.> |
---|
| 341 | * - Encapsulate: put it in another IP and send out. <unimp.> |
---|
| 342 | */ |
---|
| 343 | |
---|
| 344 | #ifdef COMPAT_IPFW |
---|
| 345 | if (ip_nat_ptr && !(*ip_nat_ptr)(&ip, &m, ifp, IP_NAT_OUT)) { |
---|
| 346 | error = EACCES; |
---|
| 347 | goto done; |
---|
| 348 | } |
---|
| 349 | |
---|
| 350 | /* |
---|
| 351 | * Check with the firewall... |
---|
| 352 | */ |
---|
| 353 | if (ip_fw_chk_ptr) { |
---|
| 354 | #ifdef IPDIVERT |
---|
| 355 | ip_divert_port = (*ip_fw_chk_ptr)(&ip, |
---|
| 356 | hlen, ifp, ip_divert_ignore, &m); |
---|
| 357 | ip_divert_ignore = 0; |
---|
| 358 | if (ip_divert_port) { /* Divert packet */ |
---|
| 359 | (*inetsw[ip_protox[IPPROTO_DIVERT]].pr_input)(m, 0); |
---|
| 360 | goto done; |
---|
| 361 | } |
---|
| 362 | #else |
---|
| 363 | /* If ipfw says divert, we have to just drop packet */ |
---|
| 364 | if ((*ip_fw_chk_ptr)(&ip, hlen, ifp, 0, &m)) { |
---|
| 365 | m_freem(m); |
---|
| 366 | goto done; |
---|
| 367 | } |
---|
| 368 | #endif |
---|
| 369 | if (!m) { |
---|
| 370 | error = EACCES; |
---|
| 371 | goto done; |
---|
| 372 | } |
---|
| 373 | } |
---|
| 374 | #endif /* COMPAT_IPFW */ |
---|
| 375 | |
---|
| 376 | /* |
---|
[0c67b436] | 377 | * If small enough for interface, or the interface will take |
---|
| 378 | * care of the fragmentation for us, we can just send directly. |
---|
[39e6e65a] | 379 | */ |
---|
| 380 | if ((u_short)ip->ip_len <= ifp->if_mtu) { |
---|
[0c67b436] | 381 | ip->ip_len = htons(ip->ip_len); |
---|
| 382 | ip->ip_off = htons(ip->ip_off); |
---|
[39e6e65a] | 383 | ip->ip_sum = 0; |
---|
[491f63a] | 384 | #ifdef _IP_VHL |
---|
[39e6e65a] | 385 | if (ip->ip_vhl == IP_VHL_BORING) { |
---|
[491f63a] | 386 | #else |
---|
| 387 | if ((ip->ip_hl == 5) && (ip->ip_v = IPVERSION)) { |
---|
| 388 | #endif |
---|
[39e6e65a] | 389 | ip->ip_sum = in_cksum_hdr(ip); |
---|
| 390 | } else { |
---|
| 391 | ip->ip_sum = in_cksum(m, hlen); |
---|
| 392 | } |
---|
| 393 | error = (*ifp->if_output)(ifp, m, |
---|
| 394 | (struct sockaddr *)dst, ro->ro_rt); |
---|
| 395 | goto done; |
---|
| 396 | } |
---|
| 397 | /* |
---|
| 398 | * Too large for interface; fragment if possible. |
---|
| 399 | * Must be able to put at least 8 bytes per fragment. |
---|
| 400 | */ |
---|
| 401 | if (ip->ip_off & IP_DF) { |
---|
| 402 | error = EMSGSIZE; |
---|
| 403 | /* |
---|
| 404 | * This case can happen if the user changed the MTU |
---|
| 405 | * of an interface after enabling IP on it. Because |
---|
| 406 | * most netifs don't keep track of routes pointing to |
---|
| 407 | * them, there is no way for one to update all its |
---|
| 408 | * routes when the MTU is changed. |
---|
| 409 | */ |
---|
| 410 | if ((ro->ro_rt->rt_flags & (RTF_UP | RTF_HOST)) |
---|
| 411 | && !(ro->ro_rt->rt_rmx.rmx_locks & RTV_MTU) |
---|
| 412 | && (ro->ro_rt->rt_rmx.rmx_mtu > ifp->if_mtu)) { |
---|
| 413 | ro->ro_rt->rt_rmx.rmx_mtu = ifp->if_mtu; |
---|
| 414 | } |
---|
| 415 | ipstat.ips_cantfrag++; |
---|
| 416 | goto bad; |
---|
| 417 | } |
---|
| 418 | len = (ifp->if_mtu - hlen) &~ 7; |
---|
| 419 | if (len < 8) { |
---|
| 420 | error = EMSGSIZE; |
---|
| 421 | goto bad; |
---|
| 422 | } |
---|
| 423 | |
---|
| 424 | { |
---|
| 425 | int mhlen, firstlen = len; |
---|
| 426 | struct mbuf **mnext = &m->m_nextpkt; |
---|
| 427 | |
---|
| 428 | /* |
---|
| 429 | * Loop through length of segment after first fragment, |
---|
| 430 | * make new header and copy data of each part and link onto chain. |
---|
| 431 | */ |
---|
| 432 | m0 = m; |
---|
| 433 | mhlen = sizeof (struct ip); |
---|
| 434 | for (off = hlen + len; off < (u_short)ip->ip_len; off += len) { |
---|
| 435 | MGETHDR(m, M_DONTWAIT, MT_HEADER); |
---|
| 436 | if (m == 0) { |
---|
| 437 | error = ENOBUFS; |
---|
| 438 | ipstat.ips_odropped++; |
---|
| 439 | goto sendorfree; |
---|
| 440 | } |
---|
[793249a] | 441 | m->m_flags |= (m0->m_flags & M_MCAST); |
---|
[39e6e65a] | 442 | m->m_data += max_linkhdr; |
---|
| 443 | mhip = mtod(m, struct ip *); |
---|
| 444 | *mhip = *ip; |
---|
| 445 | if (hlen > sizeof (struct ip)) { |
---|
| 446 | mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip); |
---|
[491f63a] | 447 | #ifdef _IP_VHL |
---|
[39e6e65a] | 448 | mhip->ip_vhl = IP_MAKE_VHL(IPVERSION, mhlen >> 2); |
---|
[491f63a] | 449 | #else |
---|
| 450 | mhip->ip_v = IPVERSION; |
---|
| 451 | mhip->ip_hl = mhlen >> 2; |
---|
| 452 | #endif |
---|
[39e6e65a] | 453 | } |
---|
| 454 | m->m_len = mhlen; |
---|
| 455 | mhip->ip_off = ((off - hlen) >> 3) + (ip->ip_off & ~IP_MF); |
---|
| 456 | if (ip->ip_off & IP_MF) |
---|
| 457 | mhip->ip_off |= IP_MF; |
---|
| 458 | if (off + len >= (u_short)ip->ip_len) |
---|
| 459 | len = (u_short)ip->ip_len - off; |
---|
| 460 | else |
---|
| 461 | mhip->ip_off |= IP_MF; |
---|
| 462 | mhip->ip_len = htons((u_short)(len + mhlen)); |
---|
| 463 | m->m_next = m_copy(m0, off, len); |
---|
| 464 | if (m->m_next == 0) { |
---|
| 465 | (void) m_free(m); |
---|
| 466 | error = ENOBUFS; /* ??? */ |
---|
| 467 | ipstat.ips_odropped++; |
---|
| 468 | goto sendorfree; |
---|
| 469 | } |
---|
| 470 | m->m_pkthdr.len = mhlen + len; |
---|
[0c67b436] | 471 | m->m_pkthdr.rcvif = NULL; |
---|
| 472 | mhip->ip_off = htons(mhip->ip_off); |
---|
[39e6e65a] | 473 | mhip->ip_sum = 0; |
---|
[491f63a] | 474 | #ifdef _IP_VHL |
---|
[39e6e65a] | 475 | if (mhip->ip_vhl == IP_VHL_BORING) { |
---|
[491f63a] | 476 | #else |
---|
| 477 | if ((mhip->ip_hl == 5) && (mhip->ip_v == IPVERSION) ) { |
---|
| 478 | #endif |
---|
[39e6e65a] | 479 | mhip->ip_sum = in_cksum_hdr(mhip); |
---|
| 480 | } else { |
---|
| 481 | mhip->ip_sum = in_cksum(m, mhlen); |
---|
| 482 | } |
---|
| 483 | *mnext = m; |
---|
| 484 | mnext = &m->m_nextpkt; |
---|
| 485 | ipstat.ips_ofragments++; |
---|
| 486 | } |
---|
| 487 | /* |
---|
| 488 | * Update first fragment by trimming what's been copied out |
---|
| 489 | * and updating header, then send each fragment (in order). |
---|
| 490 | */ |
---|
| 491 | m = m0; |
---|
| 492 | m_adj(m, hlen + firstlen - (u_short)ip->ip_len); |
---|
| 493 | m->m_pkthdr.len = hlen + firstlen; |
---|
| 494 | ip->ip_len = htons((u_short)m->m_pkthdr.len); |
---|
[0c67b436] | 495 | ip->ip_off |= IP_MF; |
---|
| 496 | ip->ip_off = htons(ip->ip_off); |
---|
[39e6e65a] | 497 | ip->ip_sum = 0; |
---|
[491f63a] | 498 | #ifdef _IP_VHL |
---|
[39e6e65a] | 499 | if (ip->ip_vhl == IP_VHL_BORING) { |
---|
[491f63a] | 500 | #else |
---|
| 501 | if ((ip->ip_hl == 5) && (ip->ip_v == IPVERSION) ) { |
---|
| 502 | #endif |
---|
[39e6e65a] | 503 | ip->ip_sum = in_cksum_hdr(ip); |
---|
| 504 | } else { |
---|
| 505 | ip->ip_sum = in_cksum(m, hlen); |
---|
| 506 | } |
---|
| 507 | sendorfree: |
---|
| 508 | for (m = m0; m; m = m0) { |
---|
| 509 | m0 = m->m_nextpkt; |
---|
| 510 | m->m_nextpkt = 0; |
---|
| 511 | if (error == 0) |
---|
| 512 | error = (*ifp->if_output)(ifp, m, |
---|
| 513 | (struct sockaddr *)dst, ro->ro_rt); |
---|
| 514 | else |
---|
| 515 | m_freem(m); |
---|
| 516 | } |
---|
| 517 | |
---|
| 518 | if (error == 0) |
---|
| 519 | ipstat.ips_fragmented++; |
---|
| 520 | } |
---|
| 521 | done: |
---|
| 522 | return (error); |
---|
| 523 | bad: |
---|
| 524 | m_freem(m0); |
---|
| 525 | goto done; |
---|
| 526 | } |
---|
| 527 | |
---|
| 528 | /* |
---|
| 529 | * Insert IP options into preformed packet. |
---|
| 530 | * Adjust IP destination as required for IP source routing, |
---|
| 531 | * as indicated by a non-zero in_addr at the start of the options. |
---|
| 532 | * |
---|
| 533 | * XXX This routine assumes that the packet has no options in place. |
---|
| 534 | */ |
---|
| 535 | static struct mbuf * |
---|
[dd967330] | 536 | ip_insertoptions(struct mbuf *m, struct mbuf *opt, int *phlen) |
---|
[39e6e65a] | 537 | { |
---|
| 538 | register struct ipoption *p = mtod(opt, struct ipoption *); |
---|
| 539 | struct mbuf *n; |
---|
| 540 | register struct ip *ip = mtod(m, struct ip *); |
---|
[387cc207] | 541 | uint32_t optlen; |
---|
[39e6e65a] | 542 | |
---|
| 543 | optlen = opt->m_len - sizeof(p->ipopt_dst); |
---|
[387cc207] | 544 | if (optlen + ip->ip_len > IP_MAXPACKET) |
---|
[39e6e65a] | 545 | return (m); /* XXX should fail */ |
---|
| 546 | if (p->ipopt_dst.s_addr) |
---|
| 547 | ip->ip_dst = p->ipopt_dst; |
---|
| 548 | if (m->m_flags & M_EXT || m->m_data - optlen < m->m_pktdat) { |
---|
| 549 | MGETHDR(n, M_DONTWAIT, MT_HEADER); |
---|
| 550 | if (n == 0) |
---|
| 551 | return (m); |
---|
| 552 | n->m_pkthdr.len = m->m_pkthdr.len + optlen; |
---|
| 553 | m->m_len -= sizeof(struct ip); |
---|
| 554 | m->m_data += sizeof(struct ip); |
---|
| 555 | n->m_next = m; |
---|
| 556 | m = n; |
---|
| 557 | m->m_len = optlen + sizeof(struct ip); |
---|
| 558 | m->m_data += max_linkhdr; |
---|
| 559 | (void)memcpy(mtod(m, void *), ip, sizeof(struct ip)); |
---|
| 560 | } else { |
---|
| 561 | m->m_data -= optlen; |
---|
| 562 | m->m_len += optlen; |
---|
| 563 | m->m_pkthdr.len += optlen; |
---|
| 564 | ovbcopy((caddr_t)ip, mtod(m, caddr_t), sizeof(struct ip)); |
---|
| 565 | } |
---|
| 566 | ip = mtod(m, struct ip *); |
---|
| 567 | bcopy(p->ipopt_list, ip + 1, optlen); |
---|
| 568 | *phlen = sizeof(struct ip) + optlen; |
---|
[491f63a] | 569 | #ifdef _IP_VHL |
---|
[39e6e65a] | 570 | ip->ip_vhl = IP_MAKE_VHL(IPVERSION, *phlen >> 2); |
---|
[491f63a] | 571 | #else |
---|
| 572 | ip->ip_v = IPVERSION; |
---|
| 573 | ip->ip_hl = *phlen >> 2; |
---|
| 574 | #endif |
---|
[39e6e65a] | 575 | ip->ip_len += optlen; |
---|
| 576 | return (m); |
---|
| 577 | } |
---|
| 578 | |
---|
| 579 | /* |
---|
| 580 | * Copy options from ip to jp, |
---|
| 581 | * omitting those not copied during fragmentation. |
---|
| 582 | */ |
---|
| 583 | static int |
---|
[dd967330] | 584 | ip_optcopy(struct ip *ip, struct ip *jp) |
---|
[39e6e65a] | 585 | { |
---|
| 586 | register u_char *cp, *dp; |
---|
| 587 | int opt, optlen, cnt; |
---|
| 588 | |
---|
| 589 | cp = (u_char *)(ip + 1); |
---|
| 590 | dp = (u_char *)(jp + 1); |
---|
[491f63a] | 591 | #ifdef _IP_VHL |
---|
[39e6e65a] | 592 | cnt = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof (struct ip); |
---|
[491f63a] | 593 | #else |
---|
| 594 | cnt = (ip->ip_hl << 2) - sizeof (struct ip); |
---|
| 595 | #endif |
---|
[39e6e65a] | 596 | for (; cnt > 0; cnt -= optlen, cp += optlen) { |
---|
| 597 | opt = cp[0]; |
---|
| 598 | if (opt == IPOPT_EOL) |
---|
| 599 | break; |
---|
| 600 | if (opt == IPOPT_NOP) { |
---|
| 601 | /* Preserve for IP mcast tunnel's LSRR alignment. */ |
---|
| 602 | *dp++ = IPOPT_NOP; |
---|
| 603 | optlen = 1; |
---|
| 604 | continue; |
---|
| 605 | } else |
---|
| 606 | optlen = cp[IPOPT_OLEN]; |
---|
| 607 | /* bogus lengths should have been caught by ip_dooptions */ |
---|
| 608 | if (optlen > cnt) |
---|
| 609 | optlen = cnt; |
---|
| 610 | if (IPOPT_COPIED(opt)) { |
---|
| 611 | bcopy(cp, dp, optlen); |
---|
| 612 | dp += optlen; |
---|
| 613 | } |
---|
| 614 | } |
---|
| 615 | for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++) |
---|
| 616 | *dp++ = IPOPT_EOL; |
---|
| 617 | return (optlen); |
---|
| 618 | } |
---|
| 619 | |
---|
| 620 | /* |
---|
| 621 | * IP socket option processing. |
---|
| 622 | */ |
---|
| 623 | int |
---|
[dd967330] | 624 | ip_ctloutput(int op, struct socket *so, int level, int optname, |
---|
| 625 | struct mbuf **mp) |
---|
[39e6e65a] | 626 | { |
---|
[0c67b436] | 627 | struct inpcb *inp = sotoinpcb(so); |
---|
[39e6e65a] | 628 | register struct mbuf *m = *mp; |
---|
| 629 | register int optval = 0; |
---|
| 630 | int error = 0; |
---|
| 631 | |
---|
| 632 | if (level != IPPROTO_IP) { |
---|
| 633 | error = EINVAL; |
---|
| 634 | if (op == PRCO_SETOPT && *mp) |
---|
| 635 | (void) m_free(*mp); |
---|
| 636 | } else switch (op) { |
---|
| 637 | |
---|
| 638 | case PRCO_SETOPT: |
---|
| 639 | switch (optname) { |
---|
| 640 | case IP_OPTIONS: |
---|
| 641 | #ifdef notyet |
---|
| 642 | case IP_RETOPTS: |
---|
| 643 | return (ip_pcbopts(optname, &inp->inp_options, m)); |
---|
| 644 | #else |
---|
| 645 | return (ip_pcbopts(&inp->inp_options, m)); |
---|
| 646 | #endif |
---|
| 647 | |
---|
| 648 | case IP_TOS: |
---|
| 649 | case IP_TTL: |
---|
| 650 | case IP_RECVOPTS: |
---|
| 651 | case IP_RECVRETOPTS: |
---|
| 652 | case IP_RECVDSTADDR: |
---|
| 653 | case IP_RECVIF: |
---|
| 654 | if (m == 0 || m->m_len != sizeof(int)) |
---|
| 655 | error = EINVAL; |
---|
| 656 | else { |
---|
| 657 | optval = *mtod(m, int *); |
---|
| 658 | switch (optname) { |
---|
| 659 | |
---|
| 660 | case IP_TOS: |
---|
| 661 | inp->inp_ip_tos = optval; |
---|
| 662 | break; |
---|
| 663 | |
---|
| 664 | case IP_TTL: |
---|
| 665 | inp->inp_ip_ttl = optval; |
---|
| 666 | break; |
---|
| 667 | #define OPTSET(bit) \ |
---|
| 668 | if (optval) \ |
---|
| 669 | inp->inp_flags |= bit; \ |
---|
| 670 | else \ |
---|
| 671 | inp->inp_flags &= ~bit; |
---|
| 672 | |
---|
| 673 | case IP_RECVOPTS: |
---|
| 674 | OPTSET(INP_RECVOPTS); |
---|
| 675 | break; |
---|
| 676 | |
---|
| 677 | case IP_RECVRETOPTS: |
---|
| 678 | OPTSET(INP_RECVRETOPTS); |
---|
| 679 | break; |
---|
| 680 | |
---|
| 681 | case IP_RECVDSTADDR: |
---|
| 682 | OPTSET(INP_RECVDSTADDR); |
---|
| 683 | break; |
---|
| 684 | |
---|
| 685 | case IP_RECVIF: |
---|
| 686 | OPTSET(INP_RECVIF); |
---|
| 687 | break; |
---|
| 688 | } |
---|
| 689 | } |
---|
| 690 | break; |
---|
| 691 | #undef OPTSET |
---|
| 692 | |
---|
| 693 | case IP_MULTICAST_IF: |
---|
| 694 | case IP_MULTICAST_VIF: |
---|
| 695 | case IP_MULTICAST_TTL: |
---|
| 696 | case IP_MULTICAST_LOOP: |
---|
| 697 | case IP_ADD_MEMBERSHIP: |
---|
| 698 | case IP_DROP_MEMBERSHIP: |
---|
| 699 | error = ip_setmoptions(optname, &inp->inp_moptions, m); |
---|
| 700 | break; |
---|
| 701 | |
---|
| 702 | case IP_PORTRANGE: |
---|
| 703 | if (m == 0 || m->m_len != sizeof(int)) |
---|
| 704 | error = EINVAL; |
---|
| 705 | else { |
---|
| 706 | optval = *mtod(m, int *); |
---|
| 707 | |
---|
| 708 | switch (optval) { |
---|
| 709 | |
---|
| 710 | case IP_PORTRANGE_DEFAULT: |
---|
| 711 | inp->inp_flags &= ~(INP_LOWPORT); |
---|
| 712 | inp->inp_flags &= ~(INP_HIGHPORT); |
---|
| 713 | break; |
---|
| 714 | |
---|
| 715 | case IP_PORTRANGE_HIGH: |
---|
| 716 | inp->inp_flags &= ~(INP_LOWPORT); |
---|
| 717 | inp->inp_flags |= INP_HIGHPORT; |
---|
| 718 | break; |
---|
| 719 | |
---|
| 720 | case IP_PORTRANGE_LOW: |
---|
| 721 | inp->inp_flags &= ~(INP_HIGHPORT); |
---|
| 722 | inp->inp_flags |= INP_LOWPORT; |
---|
| 723 | break; |
---|
| 724 | |
---|
| 725 | default: |
---|
| 726 | error = EINVAL; |
---|
| 727 | break; |
---|
| 728 | } |
---|
| 729 | } |
---|
| 730 | break; |
---|
| 731 | |
---|
| 732 | default: |
---|
| 733 | error = ENOPROTOOPT; |
---|
| 734 | break; |
---|
| 735 | } |
---|
| 736 | if (m) |
---|
| 737 | (void)m_free(m); |
---|
| 738 | break; |
---|
| 739 | |
---|
| 740 | case PRCO_GETOPT: |
---|
| 741 | switch (optname) { |
---|
| 742 | case IP_OPTIONS: |
---|
| 743 | case IP_RETOPTS: |
---|
| 744 | *mp = m = m_get(M_WAIT, MT_SOOPTS); |
---|
| 745 | if (inp->inp_options) { |
---|
| 746 | m->m_len = inp->inp_options->m_len; |
---|
| 747 | bcopy(mtod(inp->inp_options, void *), |
---|
| 748 | mtod(m, void *), m->m_len); |
---|
| 749 | } else |
---|
| 750 | m->m_len = 0; |
---|
| 751 | break; |
---|
| 752 | |
---|
| 753 | case IP_TOS: |
---|
| 754 | case IP_TTL: |
---|
| 755 | case IP_RECVOPTS: |
---|
| 756 | case IP_RECVRETOPTS: |
---|
| 757 | case IP_RECVDSTADDR: |
---|
| 758 | case IP_RECVIF: |
---|
| 759 | *mp = m = m_get(M_WAIT, MT_SOOPTS); |
---|
| 760 | m->m_len = sizeof(int); |
---|
| 761 | switch (optname) { |
---|
| 762 | |
---|
| 763 | case IP_TOS: |
---|
| 764 | optval = inp->inp_ip_tos; |
---|
| 765 | break; |
---|
| 766 | |
---|
| 767 | case IP_TTL: |
---|
| 768 | optval = inp->inp_ip_ttl; |
---|
| 769 | break; |
---|
| 770 | |
---|
| 771 | #define OPTBIT(bit) (inp->inp_flags & bit ? 1 : 0) |
---|
| 772 | |
---|
| 773 | case IP_RECVOPTS: |
---|
| 774 | optval = OPTBIT(INP_RECVOPTS); |
---|
| 775 | break; |
---|
| 776 | |
---|
| 777 | case IP_RECVRETOPTS: |
---|
| 778 | optval = OPTBIT(INP_RECVRETOPTS); |
---|
| 779 | break; |
---|
| 780 | |
---|
| 781 | case IP_RECVDSTADDR: |
---|
| 782 | optval = OPTBIT(INP_RECVDSTADDR); |
---|
| 783 | break; |
---|
| 784 | |
---|
| 785 | case IP_RECVIF: |
---|
| 786 | optval = OPTBIT(INP_RECVIF); |
---|
| 787 | break; |
---|
| 788 | } |
---|
| 789 | *mtod(m, int *) = optval; |
---|
| 790 | break; |
---|
| 791 | |
---|
| 792 | case IP_MULTICAST_IF: |
---|
| 793 | case IP_MULTICAST_VIF: |
---|
| 794 | case IP_MULTICAST_TTL: |
---|
| 795 | case IP_MULTICAST_LOOP: |
---|
| 796 | case IP_ADD_MEMBERSHIP: |
---|
| 797 | case IP_DROP_MEMBERSHIP: |
---|
| 798 | error = ip_getmoptions(optname, inp->inp_moptions, mp); |
---|
| 799 | break; |
---|
| 800 | |
---|
| 801 | case IP_PORTRANGE: |
---|
| 802 | *mp = m = m_get(M_WAIT, MT_SOOPTS); |
---|
| 803 | m->m_len = sizeof(int); |
---|
| 804 | |
---|
| 805 | if (inp->inp_flags & INP_HIGHPORT) |
---|
| 806 | optval = IP_PORTRANGE_HIGH; |
---|
| 807 | else if (inp->inp_flags & INP_LOWPORT) |
---|
| 808 | optval = IP_PORTRANGE_LOW; |
---|
| 809 | else |
---|
| 810 | optval = 0; |
---|
| 811 | |
---|
| 812 | *mtod(m, int *) = optval; |
---|
| 813 | break; |
---|
| 814 | |
---|
| 815 | default: |
---|
| 816 | error = ENOPROTOOPT; |
---|
| 817 | break; |
---|
| 818 | } |
---|
| 819 | break; |
---|
| 820 | } |
---|
| 821 | return (error); |
---|
| 822 | } |
---|
| 823 | |
---|
| 824 | /* |
---|
| 825 | * Set up IP options in pcb for insertion in output packets. |
---|
| 826 | * Store in mbuf with pointer in pcbopt, adding pseudo-option |
---|
| 827 | * with destination address if source routed. |
---|
| 828 | */ |
---|
| 829 | static int |
---|
| 830 | #ifdef notyet |
---|
[dd967330] | 831 | ip_pcbopts(int optname, struct mbuf **pcbopt, struct mbuf *m) |
---|
[39e6e65a] | 832 | #else |
---|
[dd967330] | 833 | ip_pcbopts(struct mbuf **pcbopt, struct mbuf *m) |
---|
[39e6e65a] | 834 | #endif |
---|
| 835 | { |
---|
| 836 | register int cnt, optlen; |
---|
| 837 | register u_char *cp; |
---|
| 838 | u_char opt; |
---|
| 839 | |
---|
| 840 | /* turn off any old options */ |
---|
| 841 | if (*pcbopt) |
---|
| 842 | (void)m_free(*pcbopt); |
---|
| 843 | *pcbopt = 0; |
---|
| 844 | if (m == (struct mbuf *)0 || m->m_len == 0) { |
---|
| 845 | /* |
---|
| 846 | * Only turning off any previous options. |
---|
| 847 | */ |
---|
| 848 | if (m) |
---|
| 849 | (void)m_free(m); |
---|
| 850 | return (0); |
---|
| 851 | } |
---|
| 852 | |
---|
| 853 | #ifndef vax |
---|
| 854 | if (m->m_len % sizeof(long)) |
---|
| 855 | goto bad; |
---|
| 856 | #endif |
---|
| 857 | /* |
---|
| 858 | * IP first-hop destination address will be stored before |
---|
| 859 | * actual options; move other options back |
---|
| 860 | * and clear it when none present. |
---|
| 861 | */ |
---|
| 862 | if (m->m_data + m->m_len + sizeof(struct in_addr) >= &m->m_dat[MLEN]) |
---|
| 863 | goto bad; |
---|
| 864 | cnt = m->m_len; |
---|
| 865 | m->m_len += sizeof(struct in_addr); |
---|
| 866 | cp = mtod(m, u_char *) + sizeof(struct in_addr); |
---|
| 867 | ovbcopy(mtod(m, caddr_t), (caddr_t)cp, (unsigned)cnt); |
---|
| 868 | bzero(mtod(m, caddr_t), sizeof(struct in_addr)); |
---|
| 869 | |
---|
| 870 | for (; cnt > 0; cnt -= optlen, cp += optlen) { |
---|
| 871 | opt = cp[IPOPT_OPTVAL]; |
---|
| 872 | if (opt == IPOPT_EOL) |
---|
| 873 | break; |
---|
| 874 | if (opt == IPOPT_NOP) |
---|
| 875 | optlen = 1; |
---|
| 876 | else { |
---|
| 877 | optlen = cp[IPOPT_OLEN]; |
---|
| 878 | if (optlen <= IPOPT_OLEN || optlen > cnt) |
---|
| 879 | goto bad; |
---|
| 880 | } |
---|
| 881 | switch (opt) { |
---|
| 882 | |
---|
| 883 | default: |
---|
| 884 | break; |
---|
| 885 | |
---|
| 886 | case IPOPT_LSRR: |
---|
| 887 | case IPOPT_SSRR: |
---|
| 888 | /* |
---|
| 889 | * user process specifies route as: |
---|
| 890 | * ->A->B->C->D |
---|
| 891 | * D must be our final destination (but we can't |
---|
| 892 | * check that since we may not have connected yet). |
---|
| 893 | * A is first hop destination, which doesn't appear in |
---|
| 894 | * actual IP option, but is stored before the options. |
---|
| 895 | */ |
---|
| 896 | if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr)) |
---|
| 897 | goto bad; |
---|
| 898 | m->m_len -= sizeof(struct in_addr); |
---|
| 899 | cnt -= sizeof(struct in_addr); |
---|
| 900 | optlen -= sizeof(struct in_addr); |
---|
| 901 | cp[IPOPT_OLEN] = optlen; |
---|
| 902 | /* |
---|
| 903 | * Move first hop before start of options. |
---|
| 904 | */ |
---|
| 905 | bcopy((caddr_t)&cp[IPOPT_OFFSET+1], mtod(m, caddr_t), |
---|
| 906 | sizeof(struct in_addr)); |
---|
| 907 | /* |
---|
| 908 | * Then copy rest of options back |
---|
| 909 | * to close up the deleted entry. |
---|
| 910 | */ |
---|
| 911 | ovbcopy((caddr_t)(&cp[IPOPT_OFFSET+1] + |
---|
| 912 | sizeof(struct in_addr)), |
---|
| 913 | (caddr_t)&cp[IPOPT_OFFSET+1], |
---|
| 914 | (unsigned)cnt + sizeof(struct in_addr)); |
---|
| 915 | break; |
---|
| 916 | } |
---|
| 917 | } |
---|
| 918 | if (m->m_len > MAX_IPOPTLEN + sizeof(struct in_addr)) |
---|
| 919 | goto bad; |
---|
| 920 | *pcbopt = m; |
---|
| 921 | return (0); |
---|
| 922 | |
---|
| 923 | bad: |
---|
| 924 | (void)m_free(m); |
---|
| 925 | return (EINVAL); |
---|
| 926 | } |
---|
| 927 | |
---|
| 928 | /* |
---|
| 929 | * Set the IP multicast options in response to user setsockopt(). |
---|
| 930 | */ |
---|
| 931 | static int |
---|
[dd967330] | 932 | ip_setmoptions(int optname, struct ip_moptions **imop, struct mbuf *m) |
---|
[39e6e65a] | 933 | { |
---|
[0c67b436] | 934 | int error = 0; |
---|
[39e6e65a] | 935 | u_char loop; |
---|
[0c67b436] | 936 | int i; |
---|
[39e6e65a] | 937 | struct in_addr addr; |
---|
[0c67b436] | 938 | struct ip_mreq *mreq; |
---|
| 939 | struct ifnet *ifp; |
---|
| 940 | struct ip_moptions *imo = *imop; |
---|
[39e6e65a] | 941 | struct route ro; |
---|
| 942 | register struct sockaddr_in *dst; |
---|
| 943 | int s; |
---|
| 944 | |
---|
| 945 | if (imo == NULL) { |
---|
| 946 | /* |
---|
| 947 | * No multicast option buffer attached to the pcb; |
---|
| 948 | * allocate one and initialize to default values. |
---|
| 949 | */ |
---|
| 950 | imo = (struct ip_moptions*)malloc(sizeof(*imo), M_IPMOPTS, |
---|
| 951 | M_WAITOK); |
---|
| 952 | |
---|
| 953 | if (imo == NULL) |
---|
| 954 | return (ENOBUFS); |
---|
| 955 | *imop = imo; |
---|
| 956 | imo->imo_multicast_ifp = NULL; |
---|
| 957 | imo->imo_multicast_vif = -1; |
---|
| 958 | imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL; |
---|
| 959 | imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP; |
---|
| 960 | imo->imo_num_memberships = 0; |
---|
| 961 | } |
---|
| 962 | |
---|
| 963 | switch (optname) { |
---|
| 964 | /* store an index number for the vif you wanna use in the send */ |
---|
| 965 | case IP_MULTICAST_VIF: |
---|
| 966 | if (!legal_vif_num) { |
---|
| 967 | error = EOPNOTSUPP; |
---|
| 968 | break; |
---|
| 969 | } |
---|
| 970 | if (m == NULL || m->m_len != sizeof(int)) { |
---|
| 971 | error = EINVAL; |
---|
| 972 | break; |
---|
| 973 | } |
---|
| 974 | i = *(mtod(m, int *)); |
---|
| 975 | if (!legal_vif_num(i) && (i != -1)) { |
---|
| 976 | error = EINVAL; |
---|
| 977 | break; |
---|
| 978 | } |
---|
| 979 | imo->imo_multicast_vif = i; |
---|
| 980 | break; |
---|
| 981 | |
---|
| 982 | case IP_MULTICAST_IF: |
---|
| 983 | /* |
---|
| 984 | * Select the interface for outgoing multicast packets. |
---|
| 985 | */ |
---|
| 986 | if (m == NULL || m->m_len != sizeof(struct in_addr)) { |
---|
| 987 | error = EINVAL; |
---|
| 988 | break; |
---|
| 989 | } |
---|
| 990 | addr = *(mtod(m, struct in_addr *)); |
---|
| 991 | /* |
---|
| 992 | * INADDR_ANY is used to remove a previous selection. |
---|
| 993 | * When no interface is selected, a default one is |
---|
| 994 | * chosen every time a multicast packet is sent. |
---|
| 995 | */ |
---|
| 996 | if (addr.s_addr == INADDR_ANY) { |
---|
| 997 | imo->imo_multicast_ifp = NULL; |
---|
| 998 | break; |
---|
| 999 | } |
---|
| 1000 | /* |
---|
| 1001 | * The selected interface is identified by its local |
---|
| 1002 | * IP address. Find the interface and confirm that |
---|
| 1003 | * it supports multicasting. |
---|
| 1004 | */ |
---|
| 1005 | s = splimp(); |
---|
| 1006 | INADDR_TO_IFP(addr, ifp); |
---|
| 1007 | if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) { |
---|
| 1008 | splx(s); |
---|
| 1009 | error = EADDRNOTAVAIL; |
---|
| 1010 | break; |
---|
| 1011 | } |
---|
| 1012 | imo->imo_multicast_ifp = ifp; |
---|
| 1013 | splx(s); |
---|
| 1014 | break; |
---|
| 1015 | |
---|
| 1016 | case IP_MULTICAST_TTL: |
---|
| 1017 | /* |
---|
| 1018 | * Set the IP time-to-live for outgoing multicast packets. |
---|
| 1019 | */ |
---|
| 1020 | if (m == NULL || m->m_len != 1) { |
---|
| 1021 | error = EINVAL; |
---|
| 1022 | break; |
---|
| 1023 | } |
---|
| 1024 | imo->imo_multicast_ttl = *(mtod(m, u_char *)); |
---|
| 1025 | break; |
---|
| 1026 | |
---|
| 1027 | case IP_MULTICAST_LOOP: |
---|
| 1028 | /* |
---|
| 1029 | * Set the loopback flag for outgoing multicast packets. |
---|
| 1030 | * Must be zero or one. |
---|
| 1031 | */ |
---|
| 1032 | if (m == NULL || m->m_len != 1 || |
---|
| 1033 | (loop = *(mtod(m, u_char *))) > 1) { |
---|
| 1034 | error = EINVAL; |
---|
| 1035 | break; |
---|
| 1036 | } |
---|
| 1037 | imo->imo_multicast_loop = loop; |
---|
| 1038 | break; |
---|
| 1039 | |
---|
| 1040 | case IP_ADD_MEMBERSHIP: |
---|
| 1041 | /* |
---|
| 1042 | * Add a multicast group membership. |
---|
| 1043 | * Group must be a valid IP multicast address. |
---|
| 1044 | */ |
---|
| 1045 | if (m == NULL || m->m_len != sizeof(struct ip_mreq)) { |
---|
| 1046 | error = EINVAL; |
---|
| 1047 | break; |
---|
| 1048 | } |
---|
| 1049 | mreq = mtod(m, struct ip_mreq *); |
---|
| 1050 | if (!IN_MULTICAST(ntohl(mreq->imr_multiaddr.s_addr))) { |
---|
| 1051 | error = EINVAL; |
---|
| 1052 | break; |
---|
| 1053 | } |
---|
| 1054 | s = splimp(); |
---|
| 1055 | /* |
---|
| 1056 | * If no interface address was provided, use the interface of |
---|
| 1057 | * the route to the given multicast address. |
---|
| 1058 | */ |
---|
| 1059 | if (mreq->imr_interface.s_addr == INADDR_ANY) { |
---|
| 1060 | bzero((caddr_t)&ro, sizeof(ro)); |
---|
| 1061 | dst = (struct sockaddr_in *)&ro.ro_dst; |
---|
| 1062 | dst->sin_len = sizeof(*dst); |
---|
| 1063 | dst->sin_family = AF_INET; |
---|
| 1064 | dst->sin_addr = mreq->imr_multiaddr; |
---|
| 1065 | rtalloc(&ro); |
---|
| 1066 | if (ro.ro_rt == NULL) { |
---|
| 1067 | error = EADDRNOTAVAIL; |
---|
| 1068 | splx(s); |
---|
| 1069 | break; |
---|
| 1070 | } |
---|
| 1071 | ifp = ro.ro_rt->rt_ifp; |
---|
| 1072 | rtfree(ro.ro_rt); |
---|
| 1073 | } |
---|
| 1074 | else { |
---|
| 1075 | INADDR_TO_IFP(mreq->imr_interface, ifp); |
---|
| 1076 | } |
---|
| 1077 | |
---|
| 1078 | /* |
---|
| 1079 | * See if we found an interface, and confirm that it |
---|
| 1080 | * supports multicast. |
---|
| 1081 | */ |
---|
| 1082 | if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) { |
---|
| 1083 | error = EADDRNOTAVAIL; |
---|
| 1084 | splx(s); |
---|
| 1085 | break; |
---|
| 1086 | } |
---|
| 1087 | /* |
---|
| 1088 | * See if the membership already exists or if all the |
---|
| 1089 | * membership slots are full. |
---|
| 1090 | */ |
---|
| 1091 | for (i = 0; i < imo->imo_num_memberships; ++i) { |
---|
| 1092 | if (imo->imo_membership[i]->inm_ifp == ifp && |
---|
| 1093 | imo->imo_membership[i]->inm_addr.s_addr |
---|
| 1094 | == mreq->imr_multiaddr.s_addr) |
---|
| 1095 | break; |
---|
| 1096 | } |
---|
| 1097 | if (i < imo->imo_num_memberships) { |
---|
| 1098 | error = EADDRINUSE; |
---|
| 1099 | splx(s); |
---|
| 1100 | break; |
---|
| 1101 | } |
---|
| 1102 | if (i == IP_MAX_MEMBERSHIPS) { |
---|
| 1103 | error = ETOOMANYREFS; |
---|
| 1104 | splx(s); |
---|
| 1105 | break; |
---|
| 1106 | } |
---|
| 1107 | /* |
---|
| 1108 | * Everything looks good; add a new record to the multicast |
---|
| 1109 | * address list for the given interface. |
---|
| 1110 | */ |
---|
| 1111 | if ((imo->imo_membership[i] = |
---|
| 1112 | in_addmulti(&mreq->imr_multiaddr, ifp)) == NULL) { |
---|
| 1113 | error = ENOBUFS; |
---|
| 1114 | splx(s); |
---|
| 1115 | break; |
---|
| 1116 | } |
---|
| 1117 | ++imo->imo_num_memberships; |
---|
| 1118 | splx(s); |
---|
| 1119 | break; |
---|
| 1120 | |
---|
| 1121 | case IP_DROP_MEMBERSHIP: |
---|
| 1122 | /* |
---|
| 1123 | * Drop a multicast group membership. |
---|
| 1124 | * Group must be a valid IP multicast address. |
---|
| 1125 | */ |
---|
| 1126 | if (m == NULL || m->m_len != sizeof(struct ip_mreq)) { |
---|
| 1127 | error = EINVAL; |
---|
| 1128 | break; |
---|
| 1129 | } |
---|
| 1130 | mreq = mtod(m, struct ip_mreq *); |
---|
| 1131 | if (!IN_MULTICAST(ntohl(mreq->imr_multiaddr.s_addr))) { |
---|
| 1132 | error = EINVAL; |
---|
| 1133 | break; |
---|
| 1134 | } |
---|
| 1135 | |
---|
| 1136 | s = splimp(); |
---|
| 1137 | /* |
---|
| 1138 | * If an interface address was specified, get a pointer |
---|
| 1139 | * to its ifnet structure. |
---|
| 1140 | */ |
---|
| 1141 | if (mreq->imr_interface.s_addr == INADDR_ANY) |
---|
| 1142 | ifp = NULL; |
---|
| 1143 | else { |
---|
| 1144 | INADDR_TO_IFP(mreq->imr_interface, ifp); |
---|
| 1145 | if (ifp == NULL) { |
---|
| 1146 | error = EADDRNOTAVAIL; |
---|
| 1147 | splx(s); |
---|
| 1148 | break; |
---|
| 1149 | } |
---|
| 1150 | } |
---|
| 1151 | /* |
---|
| 1152 | * Find the membership in the membership array. |
---|
| 1153 | */ |
---|
| 1154 | for (i = 0; i < imo->imo_num_memberships; ++i) { |
---|
| 1155 | if ((ifp == NULL || |
---|
| 1156 | imo->imo_membership[i]->inm_ifp == ifp) && |
---|
| 1157 | imo->imo_membership[i]->inm_addr.s_addr == |
---|
| 1158 | mreq->imr_multiaddr.s_addr) |
---|
| 1159 | break; |
---|
| 1160 | } |
---|
| 1161 | if (i == imo->imo_num_memberships) { |
---|
| 1162 | error = EADDRNOTAVAIL; |
---|
| 1163 | splx(s); |
---|
| 1164 | break; |
---|
| 1165 | } |
---|
| 1166 | /* |
---|
| 1167 | * Give up the multicast address record to which the |
---|
| 1168 | * membership points. |
---|
| 1169 | */ |
---|
| 1170 | in_delmulti(imo->imo_membership[i]); |
---|
| 1171 | /* |
---|
| 1172 | * Remove the gap in the membership array. |
---|
| 1173 | */ |
---|
| 1174 | for (++i; i < imo->imo_num_memberships; ++i) |
---|
| 1175 | imo->imo_membership[i-1] = imo->imo_membership[i]; |
---|
| 1176 | --imo->imo_num_memberships; |
---|
| 1177 | splx(s); |
---|
| 1178 | break; |
---|
| 1179 | |
---|
| 1180 | default: |
---|
| 1181 | error = EOPNOTSUPP; |
---|
| 1182 | break; |
---|
| 1183 | } |
---|
| 1184 | |
---|
| 1185 | /* |
---|
| 1186 | * If all options have default values, no need to keep the mbuf. |
---|
| 1187 | */ |
---|
| 1188 | if (imo->imo_multicast_ifp == NULL && |
---|
| 1189 | imo->imo_multicast_vif == -1 && |
---|
| 1190 | imo->imo_multicast_ttl == IP_DEFAULT_MULTICAST_TTL && |
---|
| 1191 | imo->imo_multicast_loop == IP_DEFAULT_MULTICAST_LOOP && |
---|
| 1192 | imo->imo_num_memberships == 0) { |
---|
| 1193 | free(*imop, M_IPMOPTS); |
---|
| 1194 | *imop = NULL; |
---|
| 1195 | } |
---|
| 1196 | |
---|
| 1197 | return (error); |
---|
| 1198 | } |
---|
| 1199 | |
---|
| 1200 | /* |
---|
| 1201 | * Return the IP multicast options in response to user getsockopt(). |
---|
| 1202 | */ |
---|
| 1203 | static int |
---|
[dd967330] | 1204 | ip_getmoptions(int optname, struct ip_moptions *imo, |
---|
| 1205 | struct mbuf **mp) |
---|
[39e6e65a] | 1206 | { |
---|
| 1207 | u_char *ttl; |
---|
| 1208 | u_char *loop; |
---|
| 1209 | struct in_addr *addr; |
---|
| 1210 | struct in_ifaddr *ia; |
---|
| 1211 | |
---|
| 1212 | *mp = m_get(M_WAIT, MT_SOOPTS); |
---|
| 1213 | |
---|
| 1214 | switch (optname) { |
---|
| 1215 | |
---|
| 1216 | case IP_MULTICAST_VIF: |
---|
| 1217 | if (imo != NULL) |
---|
| 1218 | *(mtod(*mp, int *)) = imo->imo_multicast_vif; |
---|
| 1219 | else |
---|
| 1220 | *(mtod(*mp, int *)) = -1; |
---|
| 1221 | (*mp)->m_len = sizeof(int); |
---|
| 1222 | return(0); |
---|
| 1223 | |
---|
| 1224 | case IP_MULTICAST_IF: |
---|
| 1225 | addr = mtod(*mp, struct in_addr *); |
---|
| 1226 | (*mp)->m_len = sizeof(struct in_addr); |
---|
| 1227 | if (imo == NULL || imo->imo_multicast_ifp == NULL) |
---|
| 1228 | addr->s_addr = INADDR_ANY; |
---|
| 1229 | else { |
---|
| 1230 | IFP_TO_IA(imo->imo_multicast_ifp, ia); |
---|
| 1231 | addr->s_addr = (ia == NULL) ? INADDR_ANY |
---|
| 1232 | : IA_SIN(ia)->sin_addr.s_addr; |
---|
| 1233 | } |
---|
| 1234 | return (0); |
---|
| 1235 | |
---|
| 1236 | case IP_MULTICAST_TTL: |
---|
| 1237 | ttl = mtod(*mp, u_char *); |
---|
| 1238 | (*mp)->m_len = 1; |
---|
| 1239 | *ttl = (imo == NULL) ? IP_DEFAULT_MULTICAST_TTL |
---|
| 1240 | : imo->imo_multicast_ttl; |
---|
| 1241 | return (0); |
---|
| 1242 | |
---|
| 1243 | case IP_MULTICAST_LOOP: |
---|
| 1244 | loop = mtod(*mp, u_char *); |
---|
| 1245 | (*mp)->m_len = 1; |
---|
| 1246 | *loop = (imo == NULL) ? IP_DEFAULT_MULTICAST_LOOP |
---|
| 1247 | : imo->imo_multicast_loop; |
---|
| 1248 | return (0); |
---|
| 1249 | |
---|
| 1250 | default: |
---|
| 1251 | return (EOPNOTSUPP); |
---|
| 1252 | } |
---|
| 1253 | } |
---|
| 1254 | |
---|
| 1255 | /* |
---|
| 1256 | * Discard the IP multicast options. |
---|
| 1257 | */ |
---|
| 1258 | void |
---|
[dd967330] | 1259 | ip_freemoptions(struct ip_moptions *imo) |
---|
[39e6e65a] | 1260 | { |
---|
| 1261 | register int i; |
---|
| 1262 | |
---|
| 1263 | if (imo != NULL) { |
---|
| 1264 | for (i = 0; i < imo->imo_num_memberships; ++i) |
---|
| 1265 | in_delmulti(imo->imo_membership[i]); |
---|
| 1266 | free(imo, M_IPMOPTS); |
---|
| 1267 | } |
---|
| 1268 | } |
---|
| 1269 | |
---|
| 1270 | /* |
---|
| 1271 | * Routine called from ip_output() to loop back a copy of an IP multicast |
---|
| 1272 | * packet to the input queue of a specified interface. Note that this |
---|
| 1273 | * calls the output routine of the loopback "driver", but with an interface |
---|
| 1274 | * pointer that might NOT be a loopback interface -- evil, but easier than |
---|
| 1275 | * replicating that code here. |
---|
| 1276 | */ |
---|
| 1277 | static void |
---|
[dd967330] | 1278 | ip_mloopback(struct ifnet *ifp, struct mbuf *m, struct sockaddr_in *dst, |
---|
| 1279 | int hlen) |
---|
[39e6e65a] | 1280 | { |
---|
| 1281 | register struct ip *ip; |
---|
| 1282 | struct mbuf *copym; |
---|
| 1283 | |
---|
| 1284 | copym = m_copy(m, 0, M_COPYALL); |
---|
| 1285 | if (copym != NULL && (copym->m_flags & M_EXT || copym->m_len < hlen)) |
---|
| 1286 | copym = m_pullup(copym, hlen); |
---|
| 1287 | if (copym != NULL) { |
---|
| 1288 | /* |
---|
| 1289 | * We don't bother to fragment if the IP length is greater |
---|
| 1290 | * than the interface's MTU. Can this possibly matter? |
---|
| 1291 | */ |
---|
| 1292 | ip = mtod(copym, struct ip *); |
---|
[0c67b436] | 1293 | ip->ip_len = htons(ip->ip_len); |
---|
| 1294 | ip->ip_off = htons(ip->ip_off); |
---|
[39e6e65a] | 1295 | ip->ip_sum = 0; |
---|
[f94e799e] | 1296 | #ifdef _IP_VHL |
---|
[39e6e65a] | 1297 | if (ip->ip_vhl == IP_VHL_BORING) { |
---|
| 1298 | ip->ip_sum = in_cksum_hdr(ip); |
---|
| 1299 | } else { |
---|
| 1300 | ip->ip_sum = in_cksum(copym, hlen); |
---|
| 1301 | } |
---|
[f94e799e] | 1302 | #else |
---|
| 1303 | ip->ip_sum = in_cksum(copym, hlen); |
---|
| 1304 | #endif |
---|
[39e6e65a] | 1305 | /* |
---|
| 1306 | * NB: |
---|
| 1307 | * It's not clear whether there are any lingering |
---|
| 1308 | * reentrancy problems in other areas which might |
---|
| 1309 | * be exposed by using ip_input directly (in |
---|
| 1310 | * particular, everything which modifies the packet |
---|
| 1311 | * in-place). Yet another option is using the |
---|
| 1312 | * protosw directly to deliver the looped back |
---|
| 1313 | * packet. For the moment, we'll err on the side |
---|
| 1314 | * of safety by continuing to abuse looutput(). |
---|
| 1315 | */ |
---|
| 1316 | #ifdef notdef |
---|
| 1317 | copym->m_pkthdr.rcvif = ifp; |
---|
[f94e799e] | 1318 | ip_input(copym); |
---|
[39e6e65a] | 1319 | #else |
---|
| 1320 | (void) looutput(ifp, copym, (struct sockaddr *)dst, NULL); |
---|
| 1321 | #endif |
---|
| 1322 | } |
---|
| 1323 | } |
---|