1 /* 2 * Copyright (c) 1982, 1986, 1988, 1990 Regents of the University of California. 3 * All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 * 7 * @(#)ip_output.c 7.23 (Berkeley) 11/12/90 8 */ 9 10 #include "param.h" 11 #include "malloc.h" 12 #include "mbuf.h" 13 #include "errno.h" 14 #include "protosw.h" 15 #include "socket.h" 16 #include "socketvar.h" 17 18 #include "../net/if.h" 19 #include "../net/route.h" 20 21 #include "in.h" 22 #include "in_systm.h" 23 #include "ip.h" 24 #include "in_pcb.h" 25 #include "in_var.h" 26 #include "ip_var.h" 27 28 #ifdef vax 29 #include "machine/mtpr.h" 30 #endif 31 32 struct mbuf *ip_insertoptions(); 33 34 /* 35 * IP output. The packet in mbuf chain m contains a skeletal IP 36 * header (with len, off, ttl, proto, tos, src, dst). 37 * The mbuf chain containing the packet will be freed. 38 * The mbuf opt, if present, will not be freed. 39 */ 40 ip_output(m0, opt, ro, flags) 41 struct mbuf *m0; 42 struct mbuf *opt; 43 struct route *ro; 44 int flags; 45 { 46 register struct ip *ip, *mhip; 47 register struct ifnet *ifp; 48 register struct mbuf *m = m0; 49 register int hlen = sizeof (struct ip); 50 int len, off, error = 0; 51 struct route iproute; 52 struct sockaddr_in *dst; 53 struct in_ifaddr *ia; 54 55 #ifdef DIAGNOSTIC 56 if ((m->m_flags & M_PKTHDR) == 0) 57 panic("ip_output no HDR"); 58 #endif 59 if (opt) { 60 m = ip_insertoptions(m, opt, &len); 61 hlen = len; 62 } 63 ip = mtod(m, struct ip *); 64 /* 65 * Fill in IP header. 66 */ 67 if ((flags & IP_FORWARDING) == 0) { 68 ip->ip_v = IPVERSION; 69 ip->ip_off &= IP_DF; 70 ip->ip_id = htons(ip_id++); 71 ip->ip_hl = hlen >> 2; 72 } else { 73 hlen = ip->ip_hl << 2; 74 ipstat.ips_localout++; 75 } 76 /* 77 * Route packet. 78 */ 79 if (ro == 0) { 80 ro = &iproute; 81 bzero((caddr_t)ro, sizeof (*ro)); 82 } 83 dst = (struct sockaddr_in *)&ro->ro_dst; 84 /* 85 * If there is a cached route, 86 * check that it is to the same destination 87 * and is still up. If not, free it and try again. 88 */ 89 if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 || 90 dst->sin_addr.s_addr != ip->ip_dst.s_addr)) { 91 RTFREE(ro->ro_rt); 92 ro->ro_rt = (struct rtentry *)0; 93 } 94 if (ro->ro_rt == 0) { 95 dst->sin_family = AF_INET; 96 dst->sin_len = sizeof(*dst); 97 dst->sin_addr = ip->ip_dst; 98 } 99 /* 100 * If routing to interface only, 101 * short circuit routing lookup. 102 */ 103 if (flags & IP_ROUTETOIF) { 104 105 ia = (struct in_ifaddr *)ifa_ifwithdstaddr((struct sockaddr *)dst); 106 if (ia == 0) 107 ia = in_iaonnetof(in_netof(ip->ip_dst)); 108 if (ia == 0) { 109 error = ENETUNREACH; 110 goto bad; 111 } 112 ifp = ia->ia_ifp; 113 } else { 114 if (ro->ro_rt == 0) 115 rtalloc(ro); 116 if (ro->ro_rt == 0) { 117 error = EHOSTUNREACH; 118 goto bad; 119 } 120 ia = (struct in_ifaddr *)ro->ro_rt->rt_ifa; 121 ifp = ro->ro_rt->rt_ifp; 122 ro->ro_rt->rt_use++; 123 if (ro->ro_rt->rt_flags & RTF_GATEWAY) 124 dst = (struct sockaddr_in *)ro->ro_rt->rt_gateway; 125 } 126 #ifndef notdef 127 /* 128 * If source address not specified yet, use address 129 * of outgoing interface. 130 */ 131 if (ip->ip_src.s_addr == INADDR_ANY) 132 ip->ip_src = IA_SIN(ia)->sin_addr; 133 #endif 134 /* 135 * Look for broadcast address and 136 * and verify user is allowed to send 137 * such a packet. 138 */ 139 if (in_broadcast(dst->sin_addr)) { 140 if ((ifp->if_flags & IFF_BROADCAST) == 0) { 141 error = EADDRNOTAVAIL; 142 goto bad; 143 } 144 if ((flags & IP_ALLOWBROADCAST) == 0) { 145 error = EACCES; 146 goto bad; 147 } 148 /* don't allow broadcast messages to be fragmented */ 149 if ((u_short)ip->ip_len > ifp->if_mtu) { 150 error = EMSGSIZE; 151 goto bad; 152 } 153 m->m_flags |= M_BCAST; 154 } 155 156 /* 157 * If small enough for interface, can just send directly. 158 */ 159 if ((u_short)ip->ip_len <= ifp->if_mtu) { 160 ip->ip_len = htons((u_short)ip->ip_len); 161 ip->ip_off = htons((u_short)ip->ip_off); 162 ip->ip_sum = 0; 163 ip->ip_sum = in_cksum(m, hlen); 164 error = (*ifp->if_output)(ifp, m, 165 (struct sockaddr *)dst, ro->ro_rt); 166 goto done; 167 } 168 ipstat.ips_fragmented++; 169 /* 170 * Too large for interface; fragment if possible. 171 * Must be able to put at least 8 bytes per fragment. 172 */ 173 if (ip->ip_off & IP_DF) { 174 error = EMSGSIZE; 175 goto bad; 176 } 177 len = (ifp->if_mtu - hlen) &~ 7; 178 if (len < 8) { 179 error = EMSGSIZE; 180 goto bad; 181 } 182 183 { 184 int mhlen, firstlen = len; 185 struct mbuf **mnext = &m->m_nextpkt; 186 187 /* 188 * Loop through length of segment after first fragment, 189 * make new header and copy data of each part and link onto chain. 190 */ 191 m0 = m; 192 mhlen = sizeof (struct ip); 193 for (off = hlen + len; off < (u_short)ip->ip_len; off += len) { 194 MGETHDR(m, M_DONTWAIT, MT_HEADER); 195 if (m == 0) { 196 error = ENOBUFS; 197 goto sendorfree; 198 } 199 m->m_data += max_linkhdr; 200 mhip = mtod(m, struct ip *); 201 *mhip = *ip; 202 if (hlen > sizeof (struct ip)) { 203 mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip); 204 mhip->ip_hl = mhlen >> 2; 205 } 206 m->m_len = mhlen; 207 mhip->ip_off = ((off - hlen) >> 3) + (ip->ip_off & ~IP_MF); 208 if (ip->ip_off & IP_MF) 209 mhip->ip_off |= IP_MF; 210 if (off + len >= (u_short)ip->ip_len) 211 len = (u_short)ip->ip_len - off; 212 else 213 mhip->ip_off |= IP_MF; 214 mhip->ip_len = htons((u_short)(len + mhlen)); 215 m->m_next = m_copy(m0, off, len); 216 if (m->m_next == 0) { 217 error = ENOBUFS; /* ??? */ 218 goto sendorfree; 219 } 220 m->m_pkthdr.len = mhlen + len; 221 m->m_pkthdr.rcvif = (struct ifnet *)0; 222 mhip->ip_off = htons((u_short)mhip->ip_off); 223 mhip->ip_sum = 0; 224 mhip->ip_sum = in_cksum(m, mhlen); 225 *mnext = m; 226 mnext = &m->m_nextpkt; 227 ipstat.ips_ofragments++; 228 } 229 /* 230 * Update first fragment by trimming what's been copied out 231 * and updating header, then send each fragment (in order). 232 */ 233 m = m0; 234 m_adj(m, hlen + firstlen - (u_short)ip->ip_len); 235 m->m_pkthdr.len = hlen + firstlen; 236 ip->ip_len = htons((u_short)m->m_pkthdr.len); 237 ip->ip_off = htons((u_short)(ip->ip_off | IP_MF)); 238 ip->ip_sum = 0; 239 ip->ip_sum = in_cksum(m, hlen); 240 sendorfree: 241 for (m = m0; m; m = m0) { 242 m0 = m->m_nextpkt; 243 m->m_nextpkt = 0; 244 if (error == 0) 245 error = (*ifp->if_output)(ifp, m, 246 (struct sockaddr *)dst, ro->ro_rt); 247 else 248 m_freem(m); 249 } 250 } 251 done: 252 if (ro == &iproute && (flags & IP_ROUTETOIF) == 0 && ro->ro_rt) 253 RTFREE(ro->ro_rt); 254 return (error); 255 bad: 256 m_freem(m0); 257 goto done; 258 } 259 260 /* 261 * Insert IP options into preformed packet. 262 * Adjust IP destination as required for IP source routing, 263 * as indicated by a non-zero in_addr at the start of the options. 264 */ 265 struct mbuf * 266 ip_insertoptions(m, opt, phlen) 267 register struct mbuf *m; 268 struct mbuf *opt; 269 int *phlen; 270 { 271 register struct ipoption *p = mtod(opt, struct ipoption *); 272 struct mbuf *n; 273 register struct ip *ip = mtod(m, struct ip *); 274 unsigned optlen; 275 276 optlen = opt->m_len - sizeof(p->ipopt_dst); 277 if (optlen + (u_short)ip->ip_len > IP_MAXPACKET) 278 return (m); /* XXX should fail */ 279 if (p->ipopt_dst.s_addr) 280 ip->ip_dst = p->ipopt_dst; 281 if (m->m_flags & M_EXT || m->m_data - optlen < m->m_pktdat) { 282 MGETHDR(n, M_DONTWAIT, MT_HEADER); 283 if (n == 0) 284 return (m); 285 n->m_pkthdr.len = m->m_pkthdr.len + optlen; 286 m->m_len -= sizeof(struct ip); 287 m->m_data += sizeof(struct ip); 288 n->m_next = m; 289 m = n; 290 m->m_len = optlen + sizeof(struct ip); 291 m->m_data += max_linkhdr; 292 bcopy((caddr_t)ip, mtod(m, caddr_t), sizeof(struct ip)); 293 } else { 294 m->m_data -= optlen; 295 m->m_len += optlen; 296 m->m_pkthdr.len += optlen; 297 ovbcopy((caddr_t)ip, mtod(m, caddr_t), sizeof(struct ip)); 298 } 299 ip = mtod(m, struct ip *); 300 bcopy((caddr_t)p->ipopt_list, (caddr_t)(ip + 1), (unsigned)optlen); 301 *phlen = sizeof(struct ip) + optlen; 302 ip->ip_len += optlen; 303 return (m); 304 } 305 306 /* 307 * Copy options from ip to jp, 308 * omitting those not copied during fragmentation. 309 */ 310 ip_optcopy(ip, jp) 311 struct ip *ip, *jp; 312 { 313 register u_char *cp, *dp; 314 int opt, optlen, cnt; 315 316 cp = (u_char *)(ip + 1); 317 dp = (u_char *)(jp + 1); 318 cnt = (ip->ip_hl << 2) - sizeof (struct ip); 319 for (; cnt > 0; cnt -= optlen, cp += optlen) { 320 opt = cp[0]; 321 if (opt == IPOPT_EOL) 322 break; 323 if (opt == IPOPT_NOP) 324 optlen = 1; 325 else 326 optlen = cp[IPOPT_OLEN]; 327 /* bogus lengths should have been caught by ip_dooptions */ 328 if (optlen > cnt) 329 optlen = cnt; 330 if (IPOPT_COPIED(opt)) { 331 bcopy((caddr_t)cp, (caddr_t)dp, (unsigned)optlen); 332 dp += optlen; 333 } 334 } 335 for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++) 336 *dp++ = IPOPT_EOL; 337 return (optlen); 338 } 339 340 /* 341 * IP socket option processing. 342 */ 343 ip_ctloutput(op, so, level, optname, mp) 344 int op; 345 struct socket *so; 346 int level, optname; 347 struct mbuf **mp; 348 { 349 register struct inpcb *inp = sotoinpcb(so); 350 register struct mbuf *m = *mp; 351 register int optval; 352 int error = 0; 353 354 if (level != IPPROTO_IP) 355 error = EINVAL; 356 else switch (op) { 357 358 case PRCO_SETOPT: 359 switch (optname) { 360 case IP_OPTIONS: 361 #ifdef notyet 362 case IP_RETOPTS: 363 return (ip_pcbopts(optname, &inp->inp_options, m)); 364 #else 365 return (ip_pcbopts(&inp->inp_options, m)); 366 #endif 367 368 case IP_TOS: 369 case IP_TTL: 370 case IP_RECVOPTS: 371 case IP_RECVRETOPTS: 372 case IP_RECVDSTADDR: 373 if (m->m_len != sizeof(int)) 374 error = EINVAL; 375 else { 376 optval = *mtod(m, int *); 377 switch (optname) { 378 379 case IP_TOS: 380 inp->inp_ip.ip_tos = optval; 381 break; 382 383 case IP_TTL: 384 inp->inp_ip.ip_ttl = optval; 385 break; 386 #define OPTSET(bit) \ 387 if (optval) \ 388 inp->inp_flags |= bit; \ 389 else \ 390 inp->inp_flags &= ~bit; 391 392 case IP_RECVOPTS: 393 OPTSET(INP_RECVOPTS); 394 break; 395 396 case IP_RECVRETOPTS: 397 OPTSET(INP_RECVRETOPTS); 398 break; 399 400 case IP_RECVDSTADDR: 401 OPTSET(INP_RECVDSTADDR); 402 break; 403 } 404 } 405 break; 406 #undef OPTSET 407 408 default: 409 error = EINVAL; 410 break; 411 } 412 if (m) 413 (void)m_free(m); 414 break; 415 416 case PRCO_GETOPT: 417 switch (optname) { 418 case IP_OPTIONS: 419 case IP_RETOPTS: 420 *mp = m = m_get(M_WAIT, MT_SOOPTS); 421 if (inp->inp_options) { 422 m->m_len = inp->inp_options->m_len; 423 bcopy(mtod(inp->inp_options, caddr_t), 424 mtod(m, caddr_t), (unsigned)m->m_len); 425 } else 426 m->m_len = 0; 427 break; 428 429 case IP_TOS: 430 case IP_TTL: 431 case IP_RECVOPTS: 432 case IP_RECVRETOPTS: 433 case IP_RECVDSTADDR: 434 *mp = m = m_get(M_WAIT, MT_SOOPTS); 435 m->m_len = sizeof(int); 436 switch (optname) { 437 438 case IP_TOS: 439 optval = inp->inp_ip.ip_tos; 440 break; 441 442 case IP_TTL: 443 optval = inp->inp_ip.ip_ttl; 444 break; 445 446 #define OPTBIT(bit) (inp->inp_flags & bit ? 1 : 0) 447 448 case IP_RECVOPTS: 449 optval = OPTBIT(INP_RECVOPTS); 450 break; 451 452 case IP_RECVRETOPTS: 453 optval = OPTBIT(INP_RECVRETOPTS); 454 break; 455 456 case IP_RECVDSTADDR: 457 optval = OPTBIT(INP_RECVDSTADDR); 458 break; 459 } 460 *mtod(m, int *) = optval; 461 break; 462 463 default: 464 error = EINVAL; 465 break; 466 } 467 break; 468 } 469 return (error); 470 } 471 472 /* 473 * Set up IP options in pcb for insertion in output packets. 474 * Store in mbuf with pointer in pcbopt, adding pseudo-option 475 * with destination address if source routed. 476 */ 477 #ifdef notyet 478 ip_pcbopts(optname, pcbopt, m) 479 int optname; 480 #else 481 ip_pcbopts(pcbopt, m) 482 #endif 483 struct mbuf **pcbopt; 484 register struct mbuf *m; 485 { 486 register cnt, optlen; 487 register u_char *cp; 488 u_char opt; 489 490 /* turn off any old options */ 491 if (*pcbopt) 492 (void)m_free(*pcbopt); 493 *pcbopt = 0; 494 if (m == (struct mbuf *)0 || m->m_len == 0) { 495 /* 496 * Only turning off any previous options. 497 */ 498 if (m) 499 (void)m_free(m); 500 return (0); 501 } 502 503 #ifndef vax 504 if (m->m_len % sizeof(long)) 505 goto bad; 506 #endif 507 /* 508 * IP first-hop destination address will be stored before 509 * actual options; move other options back 510 * and clear it when none present. 511 */ 512 if (m->m_data + m->m_len + sizeof(struct in_addr) >= &m->m_dat[MLEN]) 513 goto bad; 514 cnt = m->m_len; 515 m->m_len += sizeof(struct in_addr); 516 cp = mtod(m, u_char *) + sizeof(struct in_addr); 517 ovbcopy(mtod(m, caddr_t), (caddr_t)cp, (unsigned)cnt); 518 bzero(mtod(m, caddr_t), sizeof(struct in_addr)); 519 520 for (; cnt > 0; cnt -= optlen, cp += optlen) { 521 opt = cp[IPOPT_OPTVAL]; 522 if (opt == IPOPT_EOL) 523 break; 524 if (opt == IPOPT_NOP) 525 optlen = 1; 526 else { 527 optlen = cp[IPOPT_OLEN]; 528 if (optlen <= IPOPT_OLEN || optlen > cnt) 529 goto bad; 530 } 531 switch (opt) { 532 533 default: 534 break; 535 536 case IPOPT_LSRR: 537 case IPOPT_SSRR: 538 /* 539 * user process specifies route as: 540 * ->A->B->C->D 541 * D must be our final destination (but we can't 542 * check that since we may not have connected yet). 543 * A is first hop destination, which doesn't appear in 544 * actual IP option, but is stored before the options. 545 */ 546 if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr)) 547 goto bad; 548 m->m_len -= sizeof(struct in_addr); 549 cnt -= sizeof(struct in_addr); 550 optlen -= sizeof(struct in_addr); 551 cp[IPOPT_OLEN] = optlen; 552 /* 553 * Move first hop before start of options. 554 */ 555 bcopy((caddr_t)&cp[IPOPT_OFFSET+1], mtod(m, caddr_t), 556 sizeof(struct in_addr)); 557 /* 558 * Then copy rest of options back 559 * to close up the deleted entry. 560 */ 561 ovbcopy((caddr_t)(&cp[IPOPT_OFFSET+1] + 562 sizeof(struct in_addr)), 563 (caddr_t)&cp[IPOPT_OFFSET+1], 564 (unsigned)cnt + sizeof(struct in_addr)); 565 break; 566 } 567 } 568 if (m->m_len > MAX_IPOPTLEN + sizeof(struct in_addr)) 569 goto bad; 570 *pcbopt = m; 571 return (0); 572 573 bad: 574 (void)m_free(m); 575 return (EINVAL); 576 } 577