66#include <netinet/in_systm.h> 67#include <netinet/in_var.h> 68#include <netinet/ip.h> 69#include <netinet/in_pcb.h> 70#include <netinet/ip_var.h> 71#include <netinet/ip_fw.h> 72#include <netinet/ip_icmp.h> 73#include <netinet/ip_options.h> 74#include <machine/in_cksum.h> 75#include <netinet/ip_carp.h> 76#ifdef IPSEC 77#include <netinet/ip_ipsec.h> 78#endif /* IPSEC */ 79 80#include <sys/socketvar.h> 81 82#include <security/mac/mac_framework.h> 83 84#ifdef CTASSERT 85CTASSERT(sizeof(struct ip) == 20); 86#endif 87 88struct rwlock in_ifaddr_lock; 89RW_SYSINIT(in_ifaddr_lock, &in_ifaddr_lock, "in_ifaddr_lock"); 90 91VNET_DEFINE(int, rsvp_on); 92 93VNET_DEFINE(int, ipforwarding); 94SYSCTL_VNET_INT(_net_inet_ip, IPCTL_FORWARDING, forwarding, CTLFLAG_RW, 95 &VNET_NAME(ipforwarding), 0, 96 "Enable IP forwarding between interfaces"); 97 98static VNET_DEFINE(int, ipsendredirects) = 1; /* XXX */ 99#define V_ipsendredirects VNET(ipsendredirects) 100SYSCTL_VNET_INT(_net_inet_ip, IPCTL_SENDREDIRECTS, redirect, CTLFLAG_RW, 101 &VNET_NAME(ipsendredirects), 0, 102 "Enable sending IP redirects"); 103 104static VNET_DEFINE(int, ip_keepfaith); 105#define V_ip_keepfaith VNET(ip_keepfaith) 106SYSCTL_VNET_INT(_net_inet_ip, IPCTL_KEEPFAITH, keepfaith, CTLFLAG_RW, 107 &VNET_NAME(ip_keepfaith), 0, 108 "Enable packet capture for FAITH IPv4->IPv6 translater daemon"); 109 110static VNET_DEFINE(int, ip_sendsourcequench); 111#define V_ip_sendsourcequench VNET(ip_sendsourcequench) 112SYSCTL_VNET_INT(_net_inet_ip, OID_AUTO, sendsourcequench, CTLFLAG_RW, 113 &VNET_NAME(ip_sendsourcequench), 0, 114 "Enable the transmission of source quench packets"); 115 116VNET_DEFINE(int, ip_do_randomid); 117SYSCTL_VNET_INT(_net_inet_ip, OID_AUTO, random_id, CTLFLAG_RW, 118 &VNET_NAME(ip_do_randomid), 0, 119 "Assign random ip_id values"); 120 121/* 122 * XXX - Setting ip_checkinterface mostly implements the receive side of 123 * the Strong ES model described in RFC 1122, but since the routing table 124 * and transmit implementation do not implement the Strong ES model, 125 * setting this to 1 results in an odd hybrid. 126 * 127 * XXX - ip_checkinterface currently must be disabled if you use ipnat 128 * to translate the destination address to another local interface. 129 * 130 * XXX - ip_checkinterface must be disabled if you add IP aliases 131 * to the loopback interface instead of the interface where the 132 * packets for those addresses are received. 133 */ 134static VNET_DEFINE(int, ip_checkinterface); 135#define V_ip_checkinterface VNET(ip_checkinterface) 136SYSCTL_VNET_INT(_net_inet_ip, OID_AUTO, check_interface, CTLFLAG_RW, 137 &VNET_NAME(ip_checkinterface), 0, 138 "Verify packet arrives on correct interface"); 139 140VNET_DEFINE(struct pfil_head, inet_pfil_hook); /* Packet filter hooks */ 141 142static struct netisr_handler ip_nh = { 143 .nh_name = "ip", 144 .nh_handler = ip_input, 145 .nh_proto = NETISR_IP, 146 .nh_policy = NETISR_POLICY_FLOW, 147}; 148 149extern struct domain inetdomain; 150extern struct protosw inetsw[]; 151u_char ip_protox[IPPROTO_MAX]; 152VNET_DEFINE(struct in_ifaddrhead, in_ifaddrhead); /* first inet address */ 153VNET_DEFINE(struct in_ifaddrhashhead *, in_ifaddrhashtbl); /* inet addr hash table */ 154VNET_DEFINE(u_long, in_ifaddrhmask); /* mask for hash table */ 155 156static VNET_DEFINE(uma_zone_t, ipq_zone); 157static VNET_DEFINE(TAILQ_HEAD(ipqhead, ipq), ipq[IPREASS_NHASH]); 158static struct mtx ipqlock; 159 160#define V_ipq_zone VNET(ipq_zone) 161#define V_ipq VNET(ipq) 162 163#define IPQ_LOCK() mtx_lock(&ipqlock) 164#define IPQ_UNLOCK() mtx_unlock(&ipqlock) 165#define IPQ_LOCK_INIT() mtx_init(&ipqlock, "ipqlock", NULL, MTX_DEF) 166#define IPQ_LOCK_ASSERT() mtx_assert(&ipqlock, MA_OWNED) 167 168static void maxnipq_update(void); 169static void ipq_zone_change(void *); 170static void ip_drain_locked(void); 171 172static VNET_DEFINE(int, maxnipq); /* Administrative limit on # reass queues. */ 173static VNET_DEFINE(int, nipq); /* Total # of reass queues */ 174#define V_maxnipq VNET(maxnipq) 175#define V_nipq VNET(nipq) 176SYSCTL_VNET_INT(_net_inet_ip, OID_AUTO, fragpackets, CTLFLAG_RD, 177 &VNET_NAME(nipq), 0, 178 "Current number of IPv4 fragment reassembly queue entries"); 179 180static VNET_DEFINE(int, maxfragsperpacket); 181#define V_maxfragsperpacket VNET(maxfragsperpacket) 182SYSCTL_VNET_INT(_net_inet_ip, OID_AUTO, maxfragsperpacket, CTLFLAG_RW, 183 &VNET_NAME(maxfragsperpacket), 0, 184 "Maximum number of IPv4 fragments allowed per packet"); 185 186#ifdef IPCTL_DEFMTU 187SYSCTL_INT(_net_inet_ip, IPCTL_DEFMTU, mtu, CTLFLAG_RW, 188 &ip_mtu, 0, "Default MTU"); 189#endif 190 191#ifdef IPSTEALTH 192VNET_DEFINE(int, ipstealth); 193SYSCTL_VNET_INT(_net_inet_ip, OID_AUTO, stealth, CTLFLAG_RW, 194 &VNET_NAME(ipstealth), 0, 195 "IP stealth mode, no TTL decrementation on forwarding"); 196#endif 197 198#ifdef FLOWTABLE 199static VNET_DEFINE(int, ip_output_flowtable_size) = 2048; 200VNET_DEFINE(struct flowtable *, ip_ft); 201#define V_ip_output_flowtable_size VNET(ip_output_flowtable_size) 202 203SYSCTL_VNET_INT(_net_inet_ip, OID_AUTO, output_flowtable_size, CTLFLAG_RDTUN, 204 &VNET_NAME(ip_output_flowtable_size), 2048, 205 "number of entries in the per-cpu output flow caches"); 206#endif 207 208static void ip_freef(struct ipqhead *, struct ipq *); 209 210/* 211 * IP statistics are stored in the "array" of counter(9)s. 212 */ 213VNET_PCPUSTAT_DEFINE(struct ipstat, ipstat); 214VNET_PCPUSTAT_SYSINIT(ipstat); 215SYSCTL_VNET_PCPUSTAT(_net_inet_ip, IPCTL_STATS, stats, struct ipstat, ipstat, 216 "IP statistics (struct ipstat, netinet/ip_var.h)"); 217 218#ifdef VIMAGE 219VNET_PCPUSTAT_SYSUNINIT(ipstat); 220#endif /* VIMAGE */ 221 222/* 223 * Kernel module interface for updating ipstat. The argument is an index 224 * into ipstat treated as an array. 225 */ 226void 227kmod_ipstat_inc(int statnum) 228{ 229 230 counter_u64_add(VNET(ipstat)[statnum], 1); 231} 232 233void 234kmod_ipstat_dec(int statnum) 235{ 236 237 counter_u64_add(VNET(ipstat)[statnum], -1); 238} 239 240static int 241sysctl_netinet_intr_queue_maxlen(SYSCTL_HANDLER_ARGS) 242{ 243 int error, qlimit; 244 245 netisr_getqlimit(&ip_nh, &qlimit); 246 error = sysctl_handle_int(oidp, &qlimit, 0, req); 247 if (error || !req->newptr) 248 return (error); 249 if (qlimit < 1) 250 return (EINVAL); 251 return (netisr_setqlimit(&ip_nh, qlimit)); 252} 253SYSCTL_PROC(_net_inet_ip, IPCTL_INTRQMAXLEN, intr_queue_maxlen, 254 CTLTYPE_INT|CTLFLAG_RW, 0, 0, sysctl_netinet_intr_queue_maxlen, "I", 255 "Maximum size of the IP input queue"); 256 257static int 258sysctl_netinet_intr_queue_drops(SYSCTL_HANDLER_ARGS) 259{ 260 u_int64_t qdrops_long; 261 int error, qdrops; 262 263 netisr_getqdrops(&ip_nh, &qdrops_long); 264 qdrops = qdrops_long; 265 error = sysctl_handle_int(oidp, &qdrops, 0, req); 266 if (error || !req->newptr) 267 return (error); 268 if (qdrops != 0) 269 return (EINVAL); 270 netisr_clearqdrops(&ip_nh); 271 return (0); 272} 273 274SYSCTL_PROC(_net_inet_ip, IPCTL_INTRQDROPS, intr_queue_drops, 275 CTLTYPE_INT|CTLFLAG_RD, 0, 0, sysctl_netinet_intr_queue_drops, "I", 276 "Number of packets dropped from the IP input queue"); 277 278/* 279 * IP initialization: fill in IP protocol switch table. 280 * All protocols not implemented in kernel go to raw IP protocol handler. 281 */ 282void 283ip_init(void) 284{ 285 struct protosw *pr; 286 int i; 287 288 V_ip_id = time_second & 0xffff; 289 290 TAILQ_INIT(&V_in_ifaddrhead); 291 V_in_ifaddrhashtbl = hashinit(INADDR_NHASH, M_IFADDR, &V_in_ifaddrhmask); 292 293 /* Initialize IP reassembly queue. */ 294 for (i = 0; i < IPREASS_NHASH; i++) 295 TAILQ_INIT(&V_ipq[i]); 296 V_maxnipq = nmbclusters / 32; 297 V_maxfragsperpacket = 16; 298 V_ipq_zone = uma_zcreate("ipq", sizeof(struct ipq), NULL, NULL, NULL, 299 NULL, UMA_ALIGN_PTR, 0); 300 maxnipq_update(); 301 302 /* Initialize packet filter hooks. */ 303 V_inet_pfil_hook.ph_type = PFIL_TYPE_AF; 304 V_inet_pfil_hook.ph_af = AF_INET; 305 if ((i = pfil_head_register(&V_inet_pfil_hook)) != 0) 306 printf("%s: WARNING: unable to register pfil hook, " 307 "error %d\n", __func__, i); 308 309#ifdef FLOWTABLE 310 if (TUNABLE_INT_FETCH("net.inet.ip.output_flowtable_size", 311 &V_ip_output_flowtable_size)) { 312 if (V_ip_output_flowtable_size < 256) 313 V_ip_output_flowtable_size = 256; 314 if (!powerof2(V_ip_output_flowtable_size)) { 315 printf("flowtable must be power of 2 size\n"); 316 V_ip_output_flowtable_size = 2048; 317 } 318 } else { 319 /* 320 * round up to the next power of 2 321 */ 322 V_ip_output_flowtable_size = 1 << fls((1024 + maxusers * 64)-1); 323 } 324 V_ip_ft = flowtable_alloc("ipv4", V_ip_output_flowtable_size, FL_PCPU); 325#endif 326 327 /* Skip initialization of globals for non-default instances. */ 328 if (!IS_DEFAULT_VNET(curvnet)) 329 return; 330 331 pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW); 332 if (pr == NULL) 333 panic("ip_init: PF_INET not found"); 334 335 /* Initialize the entire ip_protox[] array to IPPROTO_RAW. */ 336 for (i = 0; i < IPPROTO_MAX; i++) 337 ip_protox[i] = pr - inetsw; 338 /* 339 * Cycle through IP protocols and put them into the appropriate place 340 * in ip_protox[]. 341 */ 342 for (pr = inetdomain.dom_protosw; 343 pr < inetdomain.dom_protoswNPROTOSW; pr++) 344 if (pr->pr_domain->dom_family == PF_INET && 345 pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) { 346 /* Be careful to only index valid IP protocols. */ 347 if (pr->pr_protocol < IPPROTO_MAX) 348 ip_protox[pr->pr_protocol] = pr - inetsw; 349 } 350 351 EVENTHANDLER_REGISTER(nmbclusters_change, ipq_zone_change, 352 NULL, EVENTHANDLER_PRI_ANY); 353 354 /* Initialize various other remaining things. */ 355 IPQ_LOCK_INIT(); 356 netisr_register(&ip_nh); 357} 358 359#ifdef VIMAGE 360void 361ip_destroy(void) 362{ 363 364 /* Cleanup in_ifaddr hash table; should be empty. */ 365 hashdestroy(V_in_ifaddrhashtbl, M_IFADDR, V_in_ifaddrhmask); 366 367 IPQ_LOCK(); 368 ip_drain_locked(); 369 IPQ_UNLOCK(); 370 371 uma_zdestroy(V_ipq_zone); 372} 373#endif 374 375/* 376 * Ip input routine. Checksum and byte swap header. If fragmented 377 * try to reassemble. Process options. Pass to next level. 378 */ 379void 380ip_input(struct mbuf *m) 381{ 382 struct ip *ip = NULL; 383 struct in_ifaddr *ia = NULL; 384 struct ifaddr *ifa; 385 struct ifnet *ifp; 386 int checkif, hlen = 0; 387 uint16_t sum, ip_len; 388 int dchg = 0; /* dest changed after fw */ 389 struct in_addr odst; /* original dst address */ 390 391 M_ASSERTPKTHDR(m); 392 393 if (m->m_flags & M_FASTFWD_OURS) { 394 m->m_flags &= ~M_FASTFWD_OURS; 395 /* Set up some basics that will be used later. */ 396 ip = mtod(m, struct ip *); 397 hlen = ip->ip_hl << 2; 398 ip_len = ntohs(ip->ip_len); 399 goto ours; 400 } 401 402 IPSTAT_INC(ips_total); 403 404 if (m->m_pkthdr.len < sizeof(struct ip)) 405 goto tooshort; 406 407 if (m->m_len < sizeof (struct ip) && 408 (m = m_pullup(m, sizeof (struct ip))) == NULL) { 409 IPSTAT_INC(ips_toosmall); 410 return; 411 } 412 ip = mtod(m, struct ip *); 413 414 if (ip->ip_v != IPVERSION) { 415 IPSTAT_INC(ips_badvers); 416 goto bad; 417 } 418 419 hlen = ip->ip_hl << 2; 420 if (hlen < sizeof(struct ip)) { /* minimum header length */ 421 IPSTAT_INC(ips_badhlen); 422 goto bad; 423 } 424 if (hlen > m->m_len) { 425 if ((m = m_pullup(m, hlen)) == NULL) { 426 IPSTAT_INC(ips_badhlen); 427 return; 428 } 429 ip = mtod(m, struct ip *); 430 } 431
| 69#include <netinet/in_systm.h> 70#include <netinet/in_var.h> 71#include <netinet/ip.h> 72#include <netinet/in_pcb.h> 73#include <netinet/ip_var.h> 74#include <netinet/ip_fw.h> 75#include <netinet/ip_icmp.h> 76#include <netinet/ip_options.h> 77#include <machine/in_cksum.h> 78#include <netinet/ip_carp.h> 79#ifdef IPSEC 80#include <netinet/ip_ipsec.h> 81#endif /* IPSEC */ 82 83#include <sys/socketvar.h> 84 85#include <security/mac/mac_framework.h> 86 87#ifdef CTASSERT 88CTASSERT(sizeof(struct ip) == 20); 89#endif 90 91struct rwlock in_ifaddr_lock; 92RW_SYSINIT(in_ifaddr_lock, &in_ifaddr_lock, "in_ifaddr_lock"); 93 94VNET_DEFINE(int, rsvp_on); 95 96VNET_DEFINE(int, ipforwarding); 97SYSCTL_VNET_INT(_net_inet_ip, IPCTL_FORWARDING, forwarding, CTLFLAG_RW, 98 &VNET_NAME(ipforwarding), 0, 99 "Enable IP forwarding between interfaces"); 100 101static VNET_DEFINE(int, ipsendredirects) = 1; /* XXX */ 102#define V_ipsendredirects VNET(ipsendredirects) 103SYSCTL_VNET_INT(_net_inet_ip, IPCTL_SENDREDIRECTS, redirect, CTLFLAG_RW, 104 &VNET_NAME(ipsendredirects), 0, 105 "Enable sending IP redirects"); 106 107static VNET_DEFINE(int, ip_keepfaith); 108#define V_ip_keepfaith VNET(ip_keepfaith) 109SYSCTL_VNET_INT(_net_inet_ip, IPCTL_KEEPFAITH, keepfaith, CTLFLAG_RW, 110 &VNET_NAME(ip_keepfaith), 0, 111 "Enable packet capture for FAITH IPv4->IPv6 translater daemon"); 112 113static VNET_DEFINE(int, ip_sendsourcequench); 114#define V_ip_sendsourcequench VNET(ip_sendsourcequench) 115SYSCTL_VNET_INT(_net_inet_ip, OID_AUTO, sendsourcequench, CTLFLAG_RW, 116 &VNET_NAME(ip_sendsourcequench), 0, 117 "Enable the transmission of source quench packets"); 118 119VNET_DEFINE(int, ip_do_randomid); 120SYSCTL_VNET_INT(_net_inet_ip, OID_AUTO, random_id, CTLFLAG_RW, 121 &VNET_NAME(ip_do_randomid), 0, 122 "Assign random ip_id values"); 123 124/* 125 * XXX - Setting ip_checkinterface mostly implements the receive side of 126 * the Strong ES model described in RFC 1122, but since the routing table 127 * and transmit implementation do not implement the Strong ES model, 128 * setting this to 1 results in an odd hybrid. 129 * 130 * XXX - ip_checkinterface currently must be disabled if you use ipnat 131 * to translate the destination address to another local interface. 132 * 133 * XXX - ip_checkinterface must be disabled if you add IP aliases 134 * to the loopback interface instead of the interface where the 135 * packets for those addresses are received. 136 */ 137static VNET_DEFINE(int, ip_checkinterface); 138#define V_ip_checkinterface VNET(ip_checkinterface) 139SYSCTL_VNET_INT(_net_inet_ip, OID_AUTO, check_interface, CTLFLAG_RW, 140 &VNET_NAME(ip_checkinterface), 0, 141 "Verify packet arrives on correct interface"); 142 143VNET_DEFINE(struct pfil_head, inet_pfil_hook); /* Packet filter hooks */ 144 145static struct netisr_handler ip_nh = { 146 .nh_name = "ip", 147 .nh_handler = ip_input, 148 .nh_proto = NETISR_IP, 149 .nh_policy = NETISR_POLICY_FLOW, 150}; 151 152extern struct domain inetdomain; 153extern struct protosw inetsw[]; 154u_char ip_protox[IPPROTO_MAX]; 155VNET_DEFINE(struct in_ifaddrhead, in_ifaddrhead); /* first inet address */ 156VNET_DEFINE(struct in_ifaddrhashhead *, in_ifaddrhashtbl); /* inet addr hash table */ 157VNET_DEFINE(u_long, in_ifaddrhmask); /* mask for hash table */ 158 159static VNET_DEFINE(uma_zone_t, ipq_zone); 160static VNET_DEFINE(TAILQ_HEAD(ipqhead, ipq), ipq[IPREASS_NHASH]); 161static struct mtx ipqlock; 162 163#define V_ipq_zone VNET(ipq_zone) 164#define V_ipq VNET(ipq) 165 166#define IPQ_LOCK() mtx_lock(&ipqlock) 167#define IPQ_UNLOCK() mtx_unlock(&ipqlock) 168#define IPQ_LOCK_INIT() mtx_init(&ipqlock, "ipqlock", NULL, MTX_DEF) 169#define IPQ_LOCK_ASSERT() mtx_assert(&ipqlock, MA_OWNED) 170 171static void maxnipq_update(void); 172static void ipq_zone_change(void *); 173static void ip_drain_locked(void); 174 175static VNET_DEFINE(int, maxnipq); /* Administrative limit on # reass queues. */ 176static VNET_DEFINE(int, nipq); /* Total # of reass queues */ 177#define V_maxnipq VNET(maxnipq) 178#define V_nipq VNET(nipq) 179SYSCTL_VNET_INT(_net_inet_ip, OID_AUTO, fragpackets, CTLFLAG_RD, 180 &VNET_NAME(nipq), 0, 181 "Current number of IPv4 fragment reassembly queue entries"); 182 183static VNET_DEFINE(int, maxfragsperpacket); 184#define V_maxfragsperpacket VNET(maxfragsperpacket) 185SYSCTL_VNET_INT(_net_inet_ip, OID_AUTO, maxfragsperpacket, CTLFLAG_RW, 186 &VNET_NAME(maxfragsperpacket), 0, 187 "Maximum number of IPv4 fragments allowed per packet"); 188 189#ifdef IPCTL_DEFMTU 190SYSCTL_INT(_net_inet_ip, IPCTL_DEFMTU, mtu, CTLFLAG_RW, 191 &ip_mtu, 0, "Default MTU"); 192#endif 193 194#ifdef IPSTEALTH 195VNET_DEFINE(int, ipstealth); 196SYSCTL_VNET_INT(_net_inet_ip, OID_AUTO, stealth, CTLFLAG_RW, 197 &VNET_NAME(ipstealth), 0, 198 "IP stealth mode, no TTL decrementation on forwarding"); 199#endif 200 201#ifdef FLOWTABLE 202static VNET_DEFINE(int, ip_output_flowtable_size) = 2048; 203VNET_DEFINE(struct flowtable *, ip_ft); 204#define V_ip_output_flowtable_size VNET(ip_output_flowtable_size) 205 206SYSCTL_VNET_INT(_net_inet_ip, OID_AUTO, output_flowtable_size, CTLFLAG_RDTUN, 207 &VNET_NAME(ip_output_flowtable_size), 2048, 208 "number of entries in the per-cpu output flow caches"); 209#endif 210 211static void ip_freef(struct ipqhead *, struct ipq *); 212 213/* 214 * IP statistics are stored in the "array" of counter(9)s. 215 */ 216VNET_PCPUSTAT_DEFINE(struct ipstat, ipstat); 217VNET_PCPUSTAT_SYSINIT(ipstat); 218SYSCTL_VNET_PCPUSTAT(_net_inet_ip, IPCTL_STATS, stats, struct ipstat, ipstat, 219 "IP statistics (struct ipstat, netinet/ip_var.h)"); 220 221#ifdef VIMAGE 222VNET_PCPUSTAT_SYSUNINIT(ipstat); 223#endif /* VIMAGE */ 224 225/* 226 * Kernel module interface for updating ipstat. The argument is an index 227 * into ipstat treated as an array. 228 */ 229void 230kmod_ipstat_inc(int statnum) 231{ 232 233 counter_u64_add(VNET(ipstat)[statnum], 1); 234} 235 236void 237kmod_ipstat_dec(int statnum) 238{ 239 240 counter_u64_add(VNET(ipstat)[statnum], -1); 241} 242 243static int 244sysctl_netinet_intr_queue_maxlen(SYSCTL_HANDLER_ARGS) 245{ 246 int error, qlimit; 247 248 netisr_getqlimit(&ip_nh, &qlimit); 249 error = sysctl_handle_int(oidp, &qlimit, 0, req); 250 if (error || !req->newptr) 251 return (error); 252 if (qlimit < 1) 253 return (EINVAL); 254 return (netisr_setqlimit(&ip_nh, qlimit)); 255} 256SYSCTL_PROC(_net_inet_ip, IPCTL_INTRQMAXLEN, intr_queue_maxlen, 257 CTLTYPE_INT|CTLFLAG_RW, 0, 0, sysctl_netinet_intr_queue_maxlen, "I", 258 "Maximum size of the IP input queue"); 259 260static int 261sysctl_netinet_intr_queue_drops(SYSCTL_HANDLER_ARGS) 262{ 263 u_int64_t qdrops_long; 264 int error, qdrops; 265 266 netisr_getqdrops(&ip_nh, &qdrops_long); 267 qdrops = qdrops_long; 268 error = sysctl_handle_int(oidp, &qdrops, 0, req); 269 if (error || !req->newptr) 270 return (error); 271 if (qdrops != 0) 272 return (EINVAL); 273 netisr_clearqdrops(&ip_nh); 274 return (0); 275} 276 277SYSCTL_PROC(_net_inet_ip, IPCTL_INTRQDROPS, intr_queue_drops, 278 CTLTYPE_INT|CTLFLAG_RD, 0, 0, sysctl_netinet_intr_queue_drops, "I", 279 "Number of packets dropped from the IP input queue"); 280 281/* 282 * IP initialization: fill in IP protocol switch table. 283 * All protocols not implemented in kernel go to raw IP protocol handler. 284 */ 285void 286ip_init(void) 287{ 288 struct protosw *pr; 289 int i; 290 291 V_ip_id = time_second & 0xffff; 292 293 TAILQ_INIT(&V_in_ifaddrhead); 294 V_in_ifaddrhashtbl = hashinit(INADDR_NHASH, M_IFADDR, &V_in_ifaddrhmask); 295 296 /* Initialize IP reassembly queue. */ 297 for (i = 0; i < IPREASS_NHASH; i++) 298 TAILQ_INIT(&V_ipq[i]); 299 V_maxnipq = nmbclusters / 32; 300 V_maxfragsperpacket = 16; 301 V_ipq_zone = uma_zcreate("ipq", sizeof(struct ipq), NULL, NULL, NULL, 302 NULL, UMA_ALIGN_PTR, 0); 303 maxnipq_update(); 304 305 /* Initialize packet filter hooks. */ 306 V_inet_pfil_hook.ph_type = PFIL_TYPE_AF; 307 V_inet_pfil_hook.ph_af = AF_INET; 308 if ((i = pfil_head_register(&V_inet_pfil_hook)) != 0) 309 printf("%s: WARNING: unable to register pfil hook, " 310 "error %d\n", __func__, i); 311 312#ifdef FLOWTABLE 313 if (TUNABLE_INT_FETCH("net.inet.ip.output_flowtable_size", 314 &V_ip_output_flowtable_size)) { 315 if (V_ip_output_flowtable_size < 256) 316 V_ip_output_flowtable_size = 256; 317 if (!powerof2(V_ip_output_flowtable_size)) { 318 printf("flowtable must be power of 2 size\n"); 319 V_ip_output_flowtable_size = 2048; 320 } 321 } else { 322 /* 323 * round up to the next power of 2 324 */ 325 V_ip_output_flowtable_size = 1 << fls((1024 + maxusers * 64)-1); 326 } 327 V_ip_ft = flowtable_alloc("ipv4", V_ip_output_flowtable_size, FL_PCPU); 328#endif 329 330 /* Skip initialization of globals for non-default instances. */ 331 if (!IS_DEFAULT_VNET(curvnet)) 332 return; 333 334 pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW); 335 if (pr == NULL) 336 panic("ip_init: PF_INET not found"); 337 338 /* Initialize the entire ip_protox[] array to IPPROTO_RAW. */ 339 for (i = 0; i < IPPROTO_MAX; i++) 340 ip_protox[i] = pr - inetsw; 341 /* 342 * Cycle through IP protocols and put them into the appropriate place 343 * in ip_protox[]. 344 */ 345 for (pr = inetdomain.dom_protosw; 346 pr < inetdomain.dom_protoswNPROTOSW; pr++) 347 if (pr->pr_domain->dom_family == PF_INET && 348 pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) { 349 /* Be careful to only index valid IP protocols. */ 350 if (pr->pr_protocol < IPPROTO_MAX) 351 ip_protox[pr->pr_protocol] = pr - inetsw; 352 } 353 354 EVENTHANDLER_REGISTER(nmbclusters_change, ipq_zone_change, 355 NULL, EVENTHANDLER_PRI_ANY); 356 357 /* Initialize various other remaining things. */ 358 IPQ_LOCK_INIT(); 359 netisr_register(&ip_nh); 360} 361 362#ifdef VIMAGE 363void 364ip_destroy(void) 365{ 366 367 /* Cleanup in_ifaddr hash table; should be empty. */ 368 hashdestroy(V_in_ifaddrhashtbl, M_IFADDR, V_in_ifaddrhmask); 369 370 IPQ_LOCK(); 371 ip_drain_locked(); 372 IPQ_UNLOCK(); 373 374 uma_zdestroy(V_ipq_zone); 375} 376#endif 377 378/* 379 * Ip input routine. Checksum and byte swap header. If fragmented 380 * try to reassemble. Process options. Pass to next level. 381 */ 382void 383ip_input(struct mbuf *m) 384{ 385 struct ip *ip = NULL; 386 struct in_ifaddr *ia = NULL; 387 struct ifaddr *ifa; 388 struct ifnet *ifp; 389 int checkif, hlen = 0; 390 uint16_t sum, ip_len; 391 int dchg = 0; /* dest changed after fw */ 392 struct in_addr odst; /* original dst address */ 393 394 M_ASSERTPKTHDR(m); 395 396 if (m->m_flags & M_FASTFWD_OURS) { 397 m->m_flags &= ~M_FASTFWD_OURS; 398 /* Set up some basics that will be used later. */ 399 ip = mtod(m, struct ip *); 400 hlen = ip->ip_hl << 2; 401 ip_len = ntohs(ip->ip_len); 402 goto ours; 403 } 404 405 IPSTAT_INC(ips_total); 406 407 if (m->m_pkthdr.len < sizeof(struct ip)) 408 goto tooshort; 409 410 if (m->m_len < sizeof (struct ip) && 411 (m = m_pullup(m, sizeof (struct ip))) == NULL) { 412 IPSTAT_INC(ips_toosmall); 413 return; 414 } 415 ip = mtod(m, struct ip *); 416 417 if (ip->ip_v != IPVERSION) { 418 IPSTAT_INC(ips_badvers); 419 goto bad; 420 } 421 422 hlen = ip->ip_hl << 2; 423 if (hlen < sizeof(struct ip)) { /* minimum header length */ 424 IPSTAT_INC(ips_badhlen); 425 goto bad; 426 } 427 if (hlen > m->m_len) { 428 if ((m = m_pullup(m, hlen)) == NULL) { 429 IPSTAT_INC(ips_badhlen); 430 return; 431 } 432 ip = mtod(m, struct ip *); 433 } 434
|
432 /* 127/8 must not appear on wire - RFC1122 */ 433 ifp = m->m_pkthdr.rcvif; 434 if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET || 435 (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) { 436 if ((ifp->if_flags & IFF_LOOPBACK) == 0) { 437 IPSTAT_INC(ips_badaddr); 438 goto bad; 439 } 440 } 441 442 if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) { 443 sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID); 444 } else { 445 if (hlen == sizeof(struct ip)) { 446 sum = in_cksum_hdr(ip); 447 } else { 448 sum = in_cksum(m, hlen); 449 } 450 } 451 if (sum) { 452 IPSTAT_INC(ips_badsum); 453 goto bad; 454 } 455 456#ifdef ALTQ 457 if (altq_input != NULL && (*altq_input)(m, AF_INET) == 0) 458 /* packet is dropped by traffic conditioner */ 459 return; 460#endif 461 462 ip_len = ntohs(ip->ip_len); 463 if (ip_len < hlen) { 464 IPSTAT_INC(ips_badlen); 465 goto bad; 466 } 467 468 /* 469 * Check that the amount of data in the buffers 470 * is as at least much as the IP header would have us expect. 471 * Trim mbufs if longer than we expect. 472 * Drop packet if shorter than we expect. 473 */ 474 if (m->m_pkthdr.len < ip_len) { 475tooshort: 476 IPSTAT_INC(ips_tooshort); 477 goto bad; 478 } 479 if (m->m_pkthdr.len > ip_len) { 480 if (m->m_len == m->m_pkthdr.len) { 481 m->m_len = ip_len; 482 m->m_pkthdr.len = ip_len; 483 } else 484 m_adj(m, ip_len - m->m_pkthdr.len); 485 } 486#ifdef IPSEC 487 /* 488 * Bypass packet filtering for packets previously handled by IPsec. 489 */ 490 if (ip_ipsec_filtertunnel(m)) 491 goto passin; 492#endif /* IPSEC */ 493 494 /* 495 * Run through list of hooks for input packets. 496 * 497 * NB: Beware of the destination address changing (e.g. 498 * by NAT rewriting). When this happens, tell 499 * ip_forward to do the right thing. 500 */ 501 502 /* Jump over all PFIL processing if hooks are not active. */ 503 if (!PFIL_HOOKED(&V_inet_pfil_hook)) 504 goto passin; 505 506 odst = ip->ip_dst; 507 if (pfil_run_hooks(&V_inet_pfil_hook, &m, ifp, PFIL_IN, NULL) != 0) 508 return; 509 if (m == NULL) /* consumed by filter */ 510 return; 511 512 ip = mtod(m, struct ip *); 513 dchg = (odst.s_addr != ip->ip_dst.s_addr); 514 ifp = m->m_pkthdr.rcvif; 515 516 if (m->m_flags & M_FASTFWD_OURS) { 517 m->m_flags &= ~M_FASTFWD_OURS; 518 goto ours; 519 } 520 if (m->m_flags & M_IP_NEXTHOP) { 521 dchg = (m_tag_find(m, PACKET_TAG_IPFORWARD, NULL) != NULL); 522 if (dchg != 0) { 523 /* 524 * Directly ship the packet on. This allows 525 * forwarding packets originally destined to us 526 * to some other directly connected host. 527 */ 528 ip_forward(m, 1); 529 return; 530 } 531 } 532passin: 533 534 /* 535 * Process options and, if not destined for us, 536 * ship it on. ip_dooptions returns 1 when an 537 * error was detected (causing an icmp message 538 * to be sent and the original packet to be freed). 539 */ 540 if (hlen > sizeof (struct ip) && ip_dooptions(m, 0)) 541 return; 542 543 /* greedy RSVP, snatches any PATH packet of the RSVP protocol and no 544 * matter if it is destined to another node, or whether it is 545 * a multicast one, RSVP wants it! and prevents it from being forwarded 546 * anywhere else. Also checks if the rsvp daemon is running before 547 * grabbing the packet. 548 */ 549 if (V_rsvp_on && ip->ip_p==IPPROTO_RSVP) 550 goto ours; 551 552 /* 553 * Check our list of addresses, to see if the packet is for us. 554 * If we don't have any addresses, assume any unicast packet 555 * we receive might be for us (and let the upper layers deal 556 * with it). 557 */ 558 if (TAILQ_EMPTY(&V_in_ifaddrhead) && 559 (m->m_flags & (M_MCAST|M_BCAST)) == 0) 560 goto ours; 561 562 /* 563 * Enable a consistency check between the destination address 564 * and the arrival interface for a unicast packet (the RFC 1122 565 * strong ES model) if IP forwarding is disabled and the packet 566 * is not locally generated and the packet is not subject to 567 * 'ipfw fwd'. 568 * 569 * XXX - Checking also should be disabled if the destination 570 * address is ipnat'ed to a different interface. 571 * 572 * XXX - Checking is incompatible with IP aliases added 573 * to the loopback interface instead of the interface where 574 * the packets are received. 575 * 576 * XXX - This is the case for carp vhost IPs as well so we 577 * insert a workaround. If the packet got here, we already 578 * checked with carp_iamatch() and carp_forus(). 579 */ 580 checkif = V_ip_checkinterface && (V_ipforwarding == 0) && 581 ifp != NULL && ((ifp->if_flags & IFF_LOOPBACK) == 0) && 582 ifp->if_carp == NULL && (dchg == 0); 583 584 /* 585 * Check for exact addresses in the hash bucket. 586 */ 587 /* IN_IFADDR_RLOCK(); */ 588 LIST_FOREACH(ia, INADDR_HASH(ip->ip_dst.s_addr), ia_hash) { 589 /* 590 * If the address matches, verify that the packet 591 * arrived via the correct interface if checking is 592 * enabled. 593 */ 594 if (IA_SIN(ia)->sin_addr.s_addr == ip->ip_dst.s_addr && 595 (!checkif || ia->ia_ifp == ifp)) { 596 ifa_ref(&ia->ia_ifa); 597 /* IN_IFADDR_RUNLOCK(); */ 598 goto ours; 599 } 600 } 601 /* IN_IFADDR_RUNLOCK(); */ 602 603 /* 604 * Check for broadcast addresses. 605 * 606 * Only accept broadcast packets that arrive via the matching 607 * interface. Reception of forwarded directed broadcasts would 608 * be handled via ip_forward() and ether_output() with the loopback 609 * into the stack for SIMPLEX interfaces handled by ether_output(). 610 */ 611 if (ifp != NULL && ifp->if_flags & IFF_BROADCAST) { 612 IF_ADDR_RLOCK(ifp); 613 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 614 if (ifa->ifa_addr->sa_family != AF_INET) 615 continue; 616 ia = ifatoia(ifa); 617 if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr == 618 ip->ip_dst.s_addr) { 619 ifa_ref(ifa); 620 IF_ADDR_RUNLOCK(ifp); 621 goto ours; 622 } 623#ifdef BOOTP_COMPAT 624 if (IA_SIN(ia)->sin_addr.s_addr == INADDR_ANY) { 625 ifa_ref(ifa); 626 IF_ADDR_RUNLOCK(ifp); 627 goto ours; 628 } 629#endif 630 } 631 IF_ADDR_RUNLOCK(ifp); 632 ia = NULL; 633 } 634 /* RFC 3927 2.7: Do not forward datagrams for 169.254.0.0/16. */ 635 if (IN_LINKLOCAL(ntohl(ip->ip_dst.s_addr))) { 636 IPSTAT_INC(ips_cantforward); 637 m_freem(m); 638 return; 639 } 640 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) { 641 if (V_ip_mrouter) { 642 /* 643 * If we are acting as a multicast router, all 644 * incoming multicast packets are passed to the 645 * kernel-level multicast forwarding function. 646 * The packet is returned (relatively) intact; if 647 * ip_mforward() returns a non-zero value, the packet 648 * must be discarded, else it may be accepted below. 649 */ 650 if (ip_mforward && ip_mforward(ip, ifp, m, 0) != 0) { 651 IPSTAT_INC(ips_cantforward); 652 m_freem(m); 653 return; 654 } 655 656 /* 657 * The process-level routing daemon needs to receive 658 * all multicast IGMP packets, whether or not this 659 * host belongs to their destination groups. 660 */ 661 if (ip->ip_p == IPPROTO_IGMP) 662 goto ours; 663 IPSTAT_INC(ips_forward); 664 } 665 /* 666 * Assume the packet is for us, to avoid prematurely taking 667 * a lock on the in_multi hash. Protocols must perform 668 * their own filtering and update statistics accordingly. 669 */ 670 goto ours; 671 } 672 if (ip->ip_dst.s_addr == (u_long)INADDR_BROADCAST) 673 goto ours; 674 if (ip->ip_dst.s_addr == INADDR_ANY) 675 goto ours; 676 677 /* 678 * FAITH(Firewall Aided Internet Translator) 679 */ 680 if (ifp && ifp->if_type == IFT_FAITH) { 681 if (V_ip_keepfaith) { 682 if (ip->ip_p == IPPROTO_TCP || ip->ip_p == IPPROTO_ICMP) 683 goto ours; 684 } 685 m_freem(m); 686 return; 687 } 688 689 /* 690 * Not for us; forward if possible and desirable. 691 */ 692 if (V_ipforwarding == 0) { 693 IPSTAT_INC(ips_cantforward); 694 m_freem(m); 695 } else { 696#ifdef IPSEC 697 if (ip_ipsec_fwd(m)) 698 goto bad; 699#endif /* IPSEC */ 700 ip_forward(m, dchg); 701 } 702 return; 703 704ours: 705#ifdef IPSTEALTH 706 /* 707 * IPSTEALTH: Process non-routing options only 708 * if the packet is destined for us. 709 */ 710 if (V_ipstealth && hlen > sizeof (struct ip) && ip_dooptions(m, 1)) { 711 if (ia != NULL) 712 ifa_free(&ia->ia_ifa); 713 return; 714 } 715#endif /* IPSTEALTH */ 716 717 /* Count the packet in the ip address stats */ 718 if (ia != NULL) { 719 ia->ia_ifa.if_ipackets++; 720 ia->ia_ifa.if_ibytes += m->m_pkthdr.len; 721 ifa_free(&ia->ia_ifa); 722 } 723 724 /* 725 * Attempt reassembly; if it succeeds, proceed. 726 * ip_reass() will return a different mbuf. 727 */ 728 if (ip->ip_off & htons(IP_MF | IP_OFFMASK)) { 729 m = ip_reass(m); 730 if (m == NULL) 731 return; 732 ip = mtod(m, struct ip *); 733 /* Get the header length of the reassembled packet */ 734 hlen = ip->ip_hl << 2; 735 } 736 737#ifdef IPSEC 738 /* 739 * enforce IPsec policy checking if we are seeing last header. 740 * note that we do not visit this with protocols with pcb layer 741 * code - like udp/tcp/raw ip. 742 */ 743 if (ip_ipsec_input(m)) 744 goto bad; 745#endif /* IPSEC */ 746 747 /* 748 * Switch out to protocol's input routine. 749 */ 750 IPSTAT_INC(ips_delivered); 751 752 (*inetsw[ip_protox[ip->ip_p]].pr_input)(m, hlen); 753 return; 754bad: 755 m_freem(m); 756} 757 758/* 759 * After maxnipq has been updated, propagate the change to UMA. The UMA zone 760 * max has slightly different semantics than the sysctl, for historical 761 * reasons. 762 */ 763static void 764maxnipq_update(void) 765{ 766 767 /* 768 * -1 for unlimited allocation. 769 */ 770 if (V_maxnipq < 0) 771 uma_zone_set_max(V_ipq_zone, 0); 772 /* 773 * Positive number for specific bound. 774 */ 775 if (V_maxnipq > 0) 776 uma_zone_set_max(V_ipq_zone, V_maxnipq); 777 /* 778 * Zero specifies no further fragment queue allocation -- set the 779 * bound very low, but rely on implementation elsewhere to actually 780 * prevent allocation and reclaim current queues. 781 */ 782 if (V_maxnipq == 0) 783 uma_zone_set_max(V_ipq_zone, 1); 784} 785 786static void 787ipq_zone_change(void *tag) 788{ 789 790 if (V_maxnipq > 0 && V_maxnipq < (nmbclusters / 32)) { 791 V_maxnipq = nmbclusters / 32; 792 maxnipq_update(); 793 } 794} 795 796static int 797sysctl_maxnipq(SYSCTL_HANDLER_ARGS) 798{ 799 int error, i; 800 801 i = V_maxnipq; 802 error = sysctl_handle_int(oidp, &i, 0, req); 803 if (error || !req->newptr) 804 return (error); 805 806 /* 807 * XXXRW: Might be a good idea to sanity check the argument and place 808 * an extreme upper bound. 809 */ 810 if (i < -1) 811 return (EINVAL); 812 V_maxnipq = i; 813 maxnipq_update(); 814 return (0); 815} 816 817SYSCTL_PROC(_net_inet_ip, OID_AUTO, maxfragpackets, CTLTYPE_INT|CTLFLAG_RW, 818 NULL, 0, sysctl_maxnipq, "I", 819 "Maximum number of IPv4 fragment reassembly queue entries"); 820 821/* 822 * Take incoming datagram fragment and try to reassemble it into 823 * whole datagram. If the argument is the first fragment or one 824 * in between the function will return NULL and store the mbuf 825 * in the fragment chain. If the argument is the last fragment 826 * the packet will be reassembled and the pointer to the new 827 * mbuf returned for further processing. Only m_tags attached 828 * to the first packet/fragment are preserved. 829 * The IP header is *NOT* adjusted out of iplen. 830 */ 831struct mbuf * 832ip_reass(struct mbuf *m) 833{ 834 struct ip *ip; 835 struct mbuf *p, *q, *nq, *t; 836 struct ipq *fp = NULL; 837 struct ipqhead *head; 838 int i, hlen, next; 839 u_int8_t ecn, ecn0; 840 u_short hash; 841 842 /* If maxnipq or maxfragsperpacket are 0, never accept fragments. */ 843 if (V_maxnipq == 0 || V_maxfragsperpacket == 0) { 844 IPSTAT_INC(ips_fragments); 845 IPSTAT_INC(ips_fragdropped); 846 m_freem(m); 847 return (NULL); 848 } 849 850 ip = mtod(m, struct ip *); 851 hlen = ip->ip_hl << 2; 852 853 hash = IPREASS_HASH(ip->ip_src.s_addr, ip->ip_id); 854 head = &V_ipq[hash]; 855 IPQ_LOCK(); 856 857 /* 858 * Look for queue of fragments 859 * of this datagram. 860 */ 861 TAILQ_FOREACH(fp, head, ipq_list) 862 if (ip->ip_id == fp->ipq_id && 863 ip->ip_src.s_addr == fp->ipq_src.s_addr && 864 ip->ip_dst.s_addr == fp->ipq_dst.s_addr && 865#ifdef MAC 866 mac_ipq_match(m, fp) && 867#endif 868 ip->ip_p == fp->ipq_p) 869 goto found; 870 871 fp = NULL; 872 873 /* 874 * Attempt to trim the number of allocated fragment queues if it 875 * exceeds the administrative limit. 876 */ 877 if ((V_nipq > V_maxnipq) && (V_maxnipq > 0)) { 878 /* 879 * drop something from the tail of the current queue 880 * before proceeding further 881 */ 882 struct ipq *q = TAILQ_LAST(head, ipqhead); 883 if (q == NULL) { /* gak */ 884 for (i = 0; i < IPREASS_NHASH; i++) { 885 struct ipq *r = TAILQ_LAST(&V_ipq[i], ipqhead); 886 if (r) { 887 IPSTAT_ADD(ips_fragtimeout, 888 r->ipq_nfrags); 889 ip_freef(&V_ipq[i], r); 890 break; 891 } 892 } 893 } else { 894 IPSTAT_ADD(ips_fragtimeout, q->ipq_nfrags); 895 ip_freef(head, q); 896 } 897 } 898 899found: 900 /* 901 * Adjust ip_len to not reflect header, 902 * convert offset of this to bytes. 903 */ 904 ip->ip_len = htons(ntohs(ip->ip_len) - hlen); 905 if (ip->ip_off & htons(IP_MF)) { 906 /* 907 * Make sure that fragments have a data length 908 * that's a non-zero multiple of 8 bytes. 909 */ 910 if (ip->ip_len == htons(0) || (ntohs(ip->ip_len) & 0x7) != 0) { 911 IPSTAT_INC(ips_toosmall); /* XXX */ 912 goto dropfrag; 913 } 914 m->m_flags |= M_IP_FRAG; 915 } else 916 m->m_flags &= ~M_IP_FRAG; 917 ip->ip_off = htons(ntohs(ip->ip_off) << 3); 918 919 /* 920 * Attempt reassembly; if it succeeds, proceed. 921 * ip_reass() will return a different mbuf. 922 */ 923 IPSTAT_INC(ips_fragments); 924 m->m_pkthdr.PH_loc.ptr = ip; 925 926 /* Previous ip_reass() started here. */ 927 /* 928 * Presence of header sizes in mbufs 929 * would confuse code below. 930 */ 931 m->m_data += hlen; 932 m->m_len -= hlen; 933 934 /* 935 * If first fragment to arrive, create a reassembly queue. 936 */ 937 if (fp == NULL) { 938 fp = uma_zalloc(V_ipq_zone, M_NOWAIT); 939 if (fp == NULL) 940 goto dropfrag; 941#ifdef MAC 942 if (mac_ipq_init(fp, M_NOWAIT) != 0) { 943 uma_zfree(V_ipq_zone, fp); 944 fp = NULL; 945 goto dropfrag; 946 } 947 mac_ipq_create(m, fp); 948#endif 949 TAILQ_INSERT_HEAD(head, fp, ipq_list); 950 V_nipq++; 951 fp->ipq_nfrags = 1; 952 fp->ipq_ttl = IPFRAGTTL; 953 fp->ipq_p = ip->ip_p; 954 fp->ipq_id = ip->ip_id; 955 fp->ipq_src = ip->ip_src; 956 fp->ipq_dst = ip->ip_dst; 957 fp->ipq_frags = m; 958 m->m_nextpkt = NULL; 959 goto done; 960 } else { 961 fp->ipq_nfrags++; 962#ifdef MAC 963 mac_ipq_update(m, fp); 964#endif 965 } 966 967#define GETIP(m) ((struct ip*)((m)->m_pkthdr.PH_loc.ptr)) 968 969 /* 970 * Handle ECN by comparing this segment with the first one; 971 * if CE is set, do not lose CE. 972 * drop if CE and not-ECT are mixed for the same packet. 973 */ 974 ecn = ip->ip_tos & IPTOS_ECN_MASK; 975 ecn0 = GETIP(fp->ipq_frags)->ip_tos & IPTOS_ECN_MASK; 976 if (ecn == IPTOS_ECN_CE) { 977 if (ecn0 == IPTOS_ECN_NOTECT) 978 goto dropfrag; 979 if (ecn0 != IPTOS_ECN_CE) 980 GETIP(fp->ipq_frags)->ip_tos |= IPTOS_ECN_CE; 981 } 982 if (ecn == IPTOS_ECN_NOTECT && ecn0 != IPTOS_ECN_NOTECT) 983 goto dropfrag; 984 985 /* 986 * Find a segment which begins after this one does. 987 */ 988 for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt) 989 if (ntohs(GETIP(q)->ip_off) > ntohs(ip->ip_off)) 990 break; 991 992 /* 993 * If there is a preceding segment, it may provide some of 994 * our data already. If so, drop the data from the incoming 995 * segment. If it provides all of our data, drop us, otherwise 996 * stick new segment in the proper place. 997 * 998 * If some of the data is dropped from the preceding 999 * segment, then it's checksum is invalidated. 1000 */ 1001 if (p) { 1002 i = ntohs(GETIP(p)->ip_off) + ntohs(GETIP(p)->ip_len) - 1003 ntohs(ip->ip_off); 1004 if (i > 0) { 1005 if (i >= ntohs(ip->ip_len)) 1006 goto dropfrag; 1007 m_adj(m, i); 1008 m->m_pkthdr.csum_flags = 0; 1009 ip->ip_off = htons(ntohs(ip->ip_off) + i); 1010 ip->ip_len = htons(ntohs(ip->ip_len) - i); 1011 } 1012 m->m_nextpkt = p->m_nextpkt; 1013 p->m_nextpkt = m; 1014 } else { 1015 m->m_nextpkt = fp->ipq_frags; 1016 fp->ipq_frags = m; 1017 } 1018 1019 /* 1020 * While we overlap succeeding segments trim them or, 1021 * if they are completely covered, dequeue them. 1022 */ 1023 for (; q != NULL && ntohs(ip->ip_off) + ntohs(ip->ip_len) > 1024 ntohs(GETIP(q)->ip_off); q = nq) { 1025 i = (ntohs(ip->ip_off) + ntohs(ip->ip_len)) - 1026 ntohs(GETIP(q)->ip_off); 1027 if (i < ntohs(GETIP(q)->ip_len)) { 1028 GETIP(q)->ip_len = htons(ntohs(GETIP(q)->ip_len) - i); 1029 GETIP(q)->ip_off = htons(ntohs(GETIP(q)->ip_off) + i); 1030 m_adj(q, i); 1031 q->m_pkthdr.csum_flags = 0; 1032 break; 1033 } 1034 nq = q->m_nextpkt; 1035 m->m_nextpkt = nq; 1036 IPSTAT_INC(ips_fragdropped); 1037 fp->ipq_nfrags--; 1038 m_freem(q); 1039 } 1040 1041 /* 1042 * Check for complete reassembly and perform frag per packet 1043 * limiting. 1044 * 1045 * Frag limiting is performed here so that the nth frag has 1046 * a chance to complete the packet before we drop the packet. 1047 * As a result, n+1 frags are actually allowed per packet, but 1048 * only n will ever be stored. (n = maxfragsperpacket.) 1049 * 1050 */ 1051 next = 0; 1052 for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt) { 1053 if (ntohs(GETIP(q)->ip_off) != next) { 1054 if (fp->ipq_nfrags > V_maxfragsperpacket) { 1055 IPSTAT_ADD(ips_fragdropped, fp->ipq_nfrags); 1056 ip_freef(head, fp); 1057 } 1058 goto done; 1059 } 1060 next += ntohs(GETIP(q)->ip_len); 1061 } 1062 /* Make sure the last packet didn't have the IP_MF flag */ 1063 if (p->m_flags & M_IP_FRAG) { 1064 if (fp->ipq_nfrags > V_maxfragsperpacket) { 1065 IPSTAT_ADD(ips_fragdropped, fp->ipq_nfrags); 1066 ip_freef(head, fp); 1067 } 1068 goto done; 1069 } 1070 1071 /* 1072 * Reassembly is complete. Make sure the packet is a sane size. 1073 */ 1074 q = fp->ipq_frags; 1075 ip = GETIP(q); 1076 if (next + (ip->ip_hl << 2) > IP_MAXPACKET) { 1077 IPSTAT_INC(ips_toolong); 1078 IPSTAT_ADD(ips_fragdropped, fp->ipq_nfrags); 1079 ip_freef(head, fp); 1080 goto done; 1081 } 1082 1083 /* 1084 * Concatenate fragments. 1085 */ 1086 m = q; 1087 t = m->m_next; 1088 m->m_next = NULL; 1089 m_cat(m, t); 1090 nq = q->m_nextpkt; 1091 q->m_nextpkt = NULL; 1092 for (q = nq; q != NULL; q = nq) { 1093 nq = q->m_nextpkt; 1094 q->m_nextpkt = NULL; 1095 m->m_pkthdr.csum_flags &= q->m_pkthdr.csum_flags; 1096 m->m_pkthdr.csum_data += q->m_pkthdr.csum_data; 1097 m_cat(m, q); 1098 } 1099 /* 1100 * In order to do checksumming faster we do 'end-around carry' here 1101 * (and not in for{} loop), though it implies we are not going to 1102 * reassemble more than 64k fragments. 1103 */ 1104 m->m_pkthdr.csum_data = 1105 (m->m_pkthdr.csum_data & 0xffff) + (m->m_pkthdr.csum_data >> 16); 1106#ifdef MAC 1107 mac_ipq_reassemble(fp, m); 1108 mac_ipq_destroy(fp); 1109#endif 1110 1111 /* 1112 * Create header for new ip packet by modifying header of first 1113 * packet; dequeue and discard fragment reassembly header. 1114 * Make header visible. 1115 */ 1116 ip->ip_len = htons((ip->ip_hl << 2) + next); 1117 ip->ip_src = fp->ipq_src; 1118 ip->ip_dst = fp->ipq_dst; 1119 TAILQ_REMOVE(head, fp, ipq_list); 1120 V_nipq--; 1121 uma_zfree(V_ipq_zone, fp); 1122 m->m_len += (ip->ip_hl << 2); 1123 m->m_data -= (ip->ip_hl << 2); 1124 /* some debugging cruft by sklower, below, will go away soon */ 1125 if (m->m_flags & M_PKTHDR) /* XXX this should be done elsewhere */ 1126 m_fixhdr(m); 1127 IPSTAT_INC(ips_reassembled); 1128 IPQ_UNLOCK(); 1129 return (m); 1130 1131dropfrag: 1132 IPSTAT_INC(ips_fragdropped); 1133 if (fp != NULL) 1134 fp->ipq_nfrags--; 1135 m_freem(m); 1136done: 1137 IPQ_UNLOCK(); 1138 return (NULL); 1139 1140#undef GETIP 1141} 1142 1143/* 1144 * Free a fragment reassembly header and all 1145 * associated datagrams. 1146 */ 1147static void 1148ip_freef(struct ipqhead *fhp, struct ipq *fp) 1149{ 1150 struct mbuf *q; 1151 1152 IPQ_LOCK_ASSERT(); 1153 1154 while (fp->ipq_frags) { 1155 q = fp->ipq_frags; 1156 fp->ipq_frags = q->m_nextpkt; 1157 m_freem(q); 1158 } 1159 TAILQ_REMOVE(fhp, fp, ipq_list); 1160 uma_zfree(V_ipq_zone, fp); 1161 V_nipq--; 1162} 1163 1164/* 1165 * IP timer processing; 1166 * if a timer expires on a reassembly 1167 * queue, discard it. 1168 */ 1169void 1170ip_slowtimo(void) 1171{ 1172 VNET_ITERATOR_DECL(vnet_iter); 1173 struct ipq *fp; 1174 int i; 1175 1176 VNET_LIST_RLOCK_NOSLEEP(); 1177 IPQ_LOCK(); 1178 VNET_FOREACH(vnet_iter) { 1179 CURVNET_SET(vnet_iter); 1180 for (i = 0; i < IPREASS_NHASH; i++) { 1181 for(fp = TAILQ_FIRST(&V_ipq[i]); fp;) { 1182 struct ipq *fpp; 1183 1184 fpp = fp; 1185 fp = TAILQ_NEXT(fp, ipq_list); 1186 if(--fpp->ipq_ttl == 0) { 1187 IPSTAT_ADD(ips_fragtimeout, 1188 fpp->ipq_nfrags); 1189 ip_freef(&V_ipq[i], fpp); 1190 } 1191 } 1192 } 1193 /* 1194 * If we are over the maximum number of fragments 1195 * (due to the limit being lowered), drain off 1196 * enough to get down to the new limit. 1197 */ 1198 if (V_maxnipq >= 0 && V_nipq > V_maxnipq) { 1199 for (i = 0; i < IPREASS_NHASH; i++) { 1200 while (V_nipq > V_maxnipq && 1201 !TAILQ_EMPTY(&V_ipq[i])) { 1202 IPSTAT_ADD(ips_fragdropped, 1203 TAILQ_FIRST(&V_ipq[i])->ipq_nfrags); 1204 ip_freef(&V_ipq[i], 1205 TAILQ_FIRST(&V_ipq[i])); 1206 } 1207 } 1208 } 1209 CURVNET_RESTORE(); 1210 } 1211 IPQ_UNLOCK(); 1212 VNET_LIST_RUNLOCK_NOSLEEP(); 1213} 1214 1215/* 1216 * Drain off all datagram fragments. 1217 */ 1218static void 1219ip_drain_locked(void) 1220{ 1221 int i; 1222 1223 IPQ_LOCK_ASSERT(); 1224 1225 for (i = 0; i < IPREASS_NHASH; i++) { 1226 while(!TAILQ_EMPTY(&V_ipq[i])) { 1227 IPSTAT_ADD(ips_fragdropped, 1228 TAILQ_FIRST(&V_ipq[i])->ipq_nfrags); 1229 ip_freef(&V_ipq[i], TAILQ_FIRST(&V_ipq[i])); 1230 } 1231 } 1232} 1233 1234void 1235ip_drain(void) 1236{ 1237 VNET_ITERATOR_DECL(vnet_iter); 1238 1239 VNET_LIST_RLOCK_NOSLEEP(); 1240 IPQ_LOCK(); 1241 VNET_FOREACH(vnet_iter) { 1242 CURVNET_SET(vnet_iter); 1243 ip_drain_locked(); 1244 CURVNET_RESTORE(); 1245 } 1246 IPQ_UNLOCK(); 1247 VNET_LIST_RUNLOCK_NOSLEEP(); 1248 in_rtqdrain(); 1249} 1250 1251/* 1252 * The protocol to be inserted into ip_protox[] must be already registered 1253 * in inetsw[], either statically or through pf_proto_register(). 1254 */ 1255int 1256ipproto_register(short ipproto) 1257{ 1258 struct protosw *pr; 1259 1260 /* Sanity checks. */ 1261 if (ipproto <= 0 || ipproto >= IPPROTO_MAX) 1262 return (EPROTONOSUPPORT); 1263 1264 /* 1265 * The protocol slot must not be occupied by another protocol 1266 * already. An index pointing to IPPROTO_RAW is unused. 1267 */ 1268 pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW); 1269 if (pr == NULL) 1270 return (EPFNOSUPPORT); 1271 if (ip_protox[ipproto] != pr - inetsw) /* IPPROTO_RAW */ 1272 return (EEXIST); 1273 1274 /* Find the protocol position in inetsw[] and set the index. */ 1275 for (pr = inetdomain.dom_protosw; 1276 pr < inetdomain.dom_protoswNPROTOSW; pr++) { 1277 if (pr->pr_domain->dom_family == PF_INET && 1278 pr->pr_protocol && pr->pr_protocol == ipproto) { 1279 ip_protox[pr->pr_protocol] = pr - inetsw; 1280 return (0); 1281 } 1282 } 1283 return (EPROTONOSUPPORT); 1284} 1285 1286int 1287ipproto_unregister(short ipproto) 1288{ 1289 struct protosw *pr; 1290 1291 /* Sanity checks. */ 1292 if (ipproto <= 0 || ipproto >= IPPROTO_MAX) 1293 return (EPROTONOSUPPORT); 1294 1295 /* Check if the protocol was indeed registered. */ 1296 pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW); 1297 if (pr == NULL) 1298 return (EPFNOSUPPORT); 1299 if (ip_protox[ipproto] == pr - inetsw) /* IPPROTO_RAW */ 1300 return (ENOENT); 1301 1302 /* Reset the protocol slot to IPPROTO_RAW. */ 1303 ip_protox[ipproto] = pr - inetsw; 1304 return (0); 1305} 1306 1307/* 1308 * Given address of next destination (final or next hop), return (referenced) 1309 * internet address info of interface to be used to get there. 1310 */ 1311struct in_ifaddr * 1312ip_rtaddr(struct in_addr dst, u_int fibnum) 1313{ 1314 struct route sro; 1315 struct sockaddr_in *sin; 1316 struct in_ifaddr *ia; 1317 1318 bzero(&sro, sizeof(sro)); 1319 sin = (struct sockaddr_in *)&sro.ro_dst; 1320 sin->sin_family = AF_INET; 1321 sin->sin_len = sizeof(*sin); 1322 sin->sin_addr = dst; 1323 in_rtalloc_ign(&sro, 0, fibnum); 1324 1325 if (sro.ro_rt == NULL) 1326 return (NULL); 1327 1328 ia = ifatoia(sro.ro_rt->rt_ifa); 1329 ifa_ref(&ia->ia_ifa); 1330 RTFREE(sro.ro_rt); 1331 return (ia); 1332} 1333 1334u_char inetctlerrmap[PRC_NCMDS] = { 1335 0, 0, 0, 0, 1336 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH, 1337 EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED, 1338 EMSGSIZE, EHOSTUNREACH, 0, 0, 1339 0, 0, EHOSTUNREACH, 0, 1340 ENOPROTOOPT, ECONNREFUSED 1341}; 1342 1343/* 1344 * Forward a packet. If some error occurs return the sender 1345 * an icmp packet. Note we can't always generate a meaningful 1346 * icmp message because icmp doesn't have a large enough repertoire 1347 * of codes and types. 1348 * 1349 * If not forwarding, just drop the packet. This could be confusing 1350 * if ipforwarding was zero but some routing protocol was advancing 1351 * us as a gateway to somewhere. However, we must let the routing 1352 * protocol deal with that. 1353 * 1354 * The srcrt parameter indicates whether the packet is being forwarded 1355 * via a source route. 1356 */ 1357void 1358ip_forward(struct mbuf *m, int srcrt) 1359{ 1360 struct ip *ip = mtod(m, struct ip *); 1361 struct in_ifaddr *ia; 1362 struct mbuf *mcopy; 1363 struct in_addr dest; 1364 struct route ro; 1365 int error, type = 0, code = 0, mtu = 0; 1366 1367 if (m->m_flags & (M_BCAST|M_MCAST) || in_canforward(ip->ip_dst) == 0) { 1368 IPSTAT_INC(ips_cantforward); 1369 m_freem(m); 1370 return; 1371 } 1372#ifdef IPSTEALTH 1373 if (!V_ipstealth) { 1374#endif 1375 if (ip->ip_ttl <= IPTTLDEC) { 1376 icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, 1377 0, 0); 1378 return; 1379 } 1380#ifdef IPSTEALTH 1381 } 1382#endif 1383 1384 ia = ip_rtaddr(ip->ip_dst, M_GETFIB(m)); 1385#ifndef IPSEC 1386 /* 1387 * 'ia' may be NULL if there is no route for this destination. 1388 * In case of IPsec, Don't discard it just yet, but pass it to 1389 * ip_output in case of outgoing IPsec policy. 1390 */ 1391 if (!srcrt && ia == NULL) { 1392 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0); 1393 return; 1394 } 1395#endif 1396 1397 /* 1398 * Save the IP header and at most 8 bytes of the payload, 1399 * in case we need to generate an ICMP message to the src. 1400 * 1401 * XXX this can be optimized a lot by saving the data in a local 1402 * buffer on the stack (72 bytes at most), and only allocating the 1403 * mbuf if really necessary. The vast majority of the packets 1404 * are forwarded without having to send an ICMP back (either 1405 * because unnecessary, or because rate limited), so we are 1406 * really we are wasting a lot of work here. 1407 * 1408 * We don't use m_copy() because it might return a reference 1409 * to a shared cluster. Both this function and ip_output() 1410 * assume exclusive access to the IP header in `m', so any 1411 * data in a cluster may change before we reach icmp_error(). 1412 */ 1413 mcopy = m_gethdr(M_NOWAIT, m->m_type); 1414 if (mcopy != NULL && !m_dup_pkthdr(mcopy, m, M_NOWAIT)) { 1415 /* 1416 * It's probably ok if the pkthdr dup fails (because 1417 * the deep copy of the tag chain failed), but for now 1418 * be conservative and just discard the copy since 1419 * code below may some day want the tags. 1420 */ 1421 m_free(mcopy); 1422 mcopy = NULL; 1423 } 1424 if (mcopy != NULL) { 1425 mcopy->m_len = min(ntohs(ip->ip_len), M_TRAILINGSPACE(mcopy)); 1426 mcopy->m_pkthdr.len = mcopy->m_len; 1427 m_copydata(m, 0, mcopy->m_len, mtod(mcopy, caddr_t)); 1428 } 1429 1430#ifdef IPSTEALTH 1431 if (!V_ipstealth) { 1432#endif 1433 ip->ip_ttl -= IPTTLDEC; 1434#ifdef IPSTEALTH 1435 } 1436#endif 1437 1438 /* 1439 * If forwarding packet using same interface that it came in on, 1440 * perhaps should send a redirect to sender to shortcut a hop. 1441 * Only send redirect if source is sending directly to us, 1442 * and if packet was not source routed (or has any options). 1443 * Also, don't send redirect if forwarding using a default route 1444 * or a route modified by a redirect. 1445 */ 1446 dest.s_addr = 0; 1447 if (!srcrt && V_ipsendredirects && 1448 ia != NULL && ia->ia_ifp == m->m_pkthdr.rcvif) { 1449 struct sockaddr_in *sin; 1450 struct rtentry *rt; 1451 1452 bzero(&ro, sizeof(ro)); 1453 sin = (struct sockaddr_in *)&ro.ro_dst; 1454 sin->sin_family = AF_INET; 1455 sin->sin_len = sizeof(*sin); 1456 sin->sin_addr = ip->ip_dst; 1457 in_rtalloc_ign(&ro, 0, M_GETFIB(m)); 1458 1459 rt = ro.ro_rt; 1460 1461 if (rt && (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 && 1462 satosin(rt_key(rt))->sin_addr.s_addr != 0) { 1463#define RTA(rt) ((struct in_ifaddr *)(rt->rt_ifa)) 1464 u_long src = ntohl(ip->ip_src.s_addr); 1465 1466 if (RTA(rt) && 1467 (src & RTA(rt)->ia_subnetmask) == RTA(rt)->ia_subnet) { 1468 if (rt->rt_flags & RTF_GATEWAY) 1469 dest.s_addr = satosin(rt->rt_gateway)->sin_addr.s_addr; 1470 else 1471 dest.s_addr = ip->ip_dst.s_addr; 1472 /* Router requirements says to only send host redirects */ 1473 type = ICMP_REDIRECT; 1474 code = ICMP_REDIRECT_HOST; 1475 } 1476 } 1477 if (rt) 1478 RTFREE(rt); 1479 } 1480 1481 /* 1482 * Try to cache the route MTU from ip_output so we can consider it for 1483 * the ICMP_UNREACH_NEEDFRAG "Next-Hop MTU" field described in RFC1191. 1484 */ 1485 bzero(&ro, sizeof(ro)); 1486 1487 error = ip_output(m, NULL, &ro, IP_FORWARDING, NULL, NULL); 1488 1489 if (error == EMSGSIZE && ro.ro_rt) 1490 mtu = ro.ro_rt->rt_rmx.rmx_mtu; 1491 RO_RTFREE(&ro); 1492 1493 if (error) 1494 IPSTAT_INC(ips_cantforward); 1495 else { 1496 IPSTAT_INC(ips_forward); 1497 if (type) 1498 IPSTAT_INC(ips_redirectsent); 1499 else { 1500 if (mcopy) 1501 m_freem(mcopy); 1502 if (ia != NULL) 1503 ifa_free(&ia->ia_ifa); 1504 return; 1505 } 1506 } 1507 if (mcopy == NULL) { 1508 if (ia != NULL) 1509 ifa_free(&ia->ia_ifa); 1510 return; 1511 } 1512 1513 switch (error) { 1514 1515 case 0: /* forwarded, but need redirect */ 1516 /* type, code set above */ 1517 break; 1518 1519 case ENETUNREACH: 1520 case EHOSTUNREACH: 1521 case ENETDOWN: 1522 case EHOSTDOWN: 1523 default: 1524 type = ICMP_UNREACH; 1525 code = ICMP_UNREACH_HOST; 1526 break; 1527 1528 case EMSGSIZE: 1529 type = ICMP_UNREACH; 1530 code = ICMP_UNREACH_NEEDFRAG; 1531 1532#ifdef IPSEC 1533 /* 1534 * If IPsec is configured for this path, 1535 * override any possibly mtu value set by ip_output. 1536 */ 1537 mtu = ip_ipsec_mtu(mcopy, mtu); 1538#endif /* IPSEC */ 1539 /* 1540 * If the MTU was set before make sure we are below the 1541 * interface MTU. 1542 * If the MTU wasn't set before use the interface mtu or 1543 * fall back to the next smaller mtu step compared to the 1544 * current packet size. 1545 */ 1546 if (mtu != 0) { 1547 if (ia != NULL) 1548 mtu = min(mtu, ia->ia_ifp->if_mtu); 1549 } else { 1550 if (ia != NULL) 1551 mtu = ia->ia_ifp->if_mtu; 1552 else 1553 mtu = ip_next_mtu(ntohs(ip->ip_len), 0); 1554 } 1555 IPSTAT_INC(ips_cantfrag); 1556 break; 1557 1558 case ENOBUFS: 1559 /* 1560 * A router should not generate ICMP_SOURCEQUENCH as 1561 * required in RFC1812 Requirements for IP Version 4 Routers. 1562 * Source quench could be a big problem under DoS attacks, 1563 * or if the underlying interface is rate-limited. 1564 * Those who need source quench packets may re-enable them 1565 * via the net.inet.ip.sendsourcequench sysctl. 1566 */ 1567 if (V_ip_sendsourcequench == 0) { 1568 m_freem(mcopy); 1569 if (ia != NULL) 1570 ifa_free(&ia->ia_ifa); 1571 return; 1572 } else { 1573 type = ICMP_SOURCEQUENCH; 1574 code = 0; 1575 } 1576 break; 1577 1578 case EACCES: /* ipfw denied packet */ 1579 m_freem(mcopy); 1580 if (ia != NULL) 1581 ifa_free(&ia->ia_ifa); 1582 return; 1583 } 1584 if (ia != NULL) 1585 ifa_free(&ia->ia_ifa); 1586 icmp_error(mcopy, type, code, dest.s_addr, mtu); 1587} 1588 1589void 1590ip_savecontrol(struct inpcb *inp, struct mbuf **mp, struct ip *ip, 1591 struct mbuf *m) 1592{ 1593 1594 if (inp->inp_socket->so_options & (SO_BINTIME | SO_TIMESTAMP)) { 1595 struct bintime bt; 1596 1597 bintime(&bt); 1598 if (inp->inp_socket->so_options & SO_BINTIME) { 1599 *mp = sbcreatecontrol((caddr_t)&bt, sizeof(bt), 1600 SCM_BINTIME, SOL_SOCKET); 1601 if (*mp) 1602 mp = &(*mp)->m_next; 1603 } 1604 if (inp->inp_socket->so_options & SO_TIMESTAMP) { 1605 struct timeval tv; 1606 1607 bintime2timeval(&bt, &tv); 1608 *mp = sbcreatecontrol((caddr_t)&tv, sizeof(tv), 1609 SCM_TIMESTAMP, SOL_SOCKET); 1610 if (*mp) 1611 mp = &(*mp)->m_next; 1612 } 1613 } 1614 if (inp->inp_flags & INP_RECVDSTADDR) { 1615 *mp = sbcreatecontrol((caddr_t)&ip->ip_dst, 1616 sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP); 1617 if (*mp) 1618 mp = &(*mp)->m_next; 1619 } 1620 if (inp->inp_flags & INP_RECVTTL) { 1621 *mp = sbcreatecontrol((caddr_t)&ip->ip_ttl, 1622 sizeof(u_char), IP_RECVTTL, IPPROTO_IP); 1623 if (*mp) 1624 mp = &(*mp)->m_next; 1625 } 1626#ifdef notyet 1627 /* XXX 1628 * Moving these out of udp_input() made them even more broken 1629 * than they already were. 1630 */ 1631 /* options were tossed already */ 1632 if (inp->inp_flags & INP_RECVOPTS) { 1633 *mp = sbcreatecontrol((caddr_t)opts_deleted_above, 1634 sizeof(struct in_addr), IP_RECVOPTS, IPPROTO_IP); 1635 if (*mp) 1636 mp = &(*mp)->m_next; 1637 } 1638 /* ip_srcroute doesn't do what we want here, need to fix */ 1639 if (inp->inp_flags & INP_RECVRETOPTS) { 1640 *mp = sbcreatecontrol((caddr_t)ip_srcroute(m), 1641 sizeof(struct in_addr), IP_RECVRETOPTS, IPPROTO_IP); 1642 if (*mp) 1643 mp = &(*mp)->m_next; 1644 } 1645#endif 1646 if (inp->inp_flags & INP_RECVIF) { 1647 struct ifnet *ifp; 1648 struct sdlbuf { 1649 struct sockaddr_dl sdl; 1650 u_char pad[32]; 1651 } sdlbuf; 1652 struct sockaddr_dl *sdp; 1653 struct sockaddr_dl *sdl2 = &sdlbuf.sdl; 1654 1655 if ((ifp = m->m_pkthdr.rcvif) && 1656 ifp->if_index && ifp->if_index <= V_if_index) { 1657 sdp = (struct sockaddr_dl *)ifp->if_addr->ifa_addr; 1658 /* 1659 * Change our mind and don't try copy. 1660 */ 1661 if (sdp->sdl_family != AF_LINK || 1662 sdp->sdl_len > sizeof(sdlbuf)) { 1663 goto makedummy; 1664 } 1665 bcopy(sdp, sdl2, sdp->sdl_len); 1666 } else { 1667makedummy: 1668 sdl2->sdl_len = 1669 offsetof(struct sockaddr_dl, sdl_data[0]); 1670 sdl2->sdl_family = AF_LINK; 1671 sdl2->sdl_index = 0; 1672 sdl2->sdl_nlen = sdl2->sdl_alen = sdl2->sdl_slen = 0; 1673 } 1674 *mp = sbcreatecontrol((caddr_t)sdl2, sdl2->sdl_len, 1675 IP_RECVIF, IPPROTO_IP); 1676 if (*mp) 1677 mp = &(*mp)->m_next; 1678 } 1679 if (inp->inp_flags & INP_RECVTOS) { 1680 *mp = sbcreatecontrol((caddr_t)&ip->ip_tos, 1681 sizeof(u_char), IP_RECVTOS, IPPROTO_IP); 1682 if (*mp) 1683 mp = &(*mp)->m_next; 1684 } 1685} 1686 1687/* 1688 * XXXRW: Multicast routing code in ip_mroute.c is generally MPSAFE, but the 1689 * ip_rsvp and ip_rsvp_on variables need to be interlocked with rsvp_on 1690 * locking. This code remains in ip_input.c as ip_mroute.c is optionally 1691 * compiled. 1692 */ 1693static VNET_DEFINE(int, ip_rsvp_on); 1694VNET_DEFINE(struct socket *, ip_rsvpd); 1695 1696#define V_ip_rsvp_on VNET(ip_rsvp_on) 1697 1698int 1699ip_rsvp_init(struct socket *so) 1700{ 1701 1702 if (so->so_type != SOCK_RAW || 1703 so->so_proto->pr_protocol != IPPROTO_RSVP) 1704 return EOPNOTSUPP; 1705 1706 if (V_ip_rsvpd != NULL) 1707 return EADDRINUSE; 1708 1709 V_ip_rsvpd = so; 1710 /* 1711 * This may seem silly, but we need to be sure we don't over-increment 1712 * the RSVP counter, in case something slips up. 1713 */ 1714 if (!V_ip_rsvp_on) { 1715 V_ip_rsvp_on = 1; 1716 V_rsvp_on++; 1717 } 1718 1719 return 0; 1720} 1721 1722int 1723ip_rsvp_done(void) 1724{ 1725 1726 V_ip_rsvpd = NULL; 1727 /* 1728 * This may seem silly, but we need to be sure we don't over-decrement 1729 * the RSVP counter, in case something slips up. 1730 */ 1731 if (V_ip_rsvp_on) { 1732 V_ip_rsvp_on = 0; 1733 V_rsvp_on--; 1734 } 1735 return 0; 1736} 1737 1738void 1739rsvp_input(struct mbuf *m, int off) /* XXX must fixup manually */ 1740{ 1741 1742 if (rsvp_input_p) { /* call the real one if loaded */ 1743 rsvp_input_p(m, off); 1744 return; 1745 } 1746 1747 /* Can still get packets with rsvp_on = 0 if there is a local member 1748 * of the group to which the RSVP packet is addressed. But in this 1749 * case we want to throw the packet away. 1750 */ 1751 1752 if (!V_rsvp_on) { 1753 m_freem(m); 1754 return; 1755 } 1756 1757 if (V_ip_rsvpd != NULL) { 1758 rip_input(m, off); 1759 return; 1760 } 1761 /* Drop the packet */ 1762 m_freem(m); 1763}
| 437 /* 127/8 must not appear on wire - RFC1122 */ 438 ifp = m->m_pkthdr.rcvif; 439 if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET || 440 (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) { 441 if ((ifp->if_flags & IFF_LOOPBACK) == 0) { 442 IPSTAT_INC(ips_badaddr); 443 goto bad; 444 } 445 } 446 447 if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) { 448 sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID); 449 } else { 450 if (hlen == sizeof(struct ip)) { 451 sum = in_cksum_hdr(ip); 452 } else { 453 sum = in_cksum(m, hlen); 454 } 455 } 456 if (sum) { 457 IPSTAT_INC(ips_badsum); 458 goto bad; 459 } 460 461#ifdef ALTQ 462 if (altq_input != NULL && (*altq_input)(m, AF_INET) == 0) 463 /* packet is dropped by traffic conditioner */ 464 return; 465#endif 466 467 ip_len = ntohs(ip->ip_len); 468 if (ip_len < hlen) { 469 IPSTAT_INC(ips_badlen); 470 goto bad; 471 } 472 473 /* 474 * Check that the amount of data in the buffers 475 * is as at least much as the IP header would have us expect. 476 * Trim mbufs if longer than we expect. 477 * Drop packet if shorter than we expect. 478 */ 479 if (m->m_pkthdr.len < ip_len) { 480tooshort: 481 IPSTAT_INC(ips_tooshort); 482 goto bad; 483 } 484 if (m->m_pkthdr.len > ip_len) { 485 if (m->m_len == m->m_pkthdr.len) { 486 m->m_len = ip_len; 487 m->m_pkthdr.len = ip_len; 488 } else 489 m_adj(m, ip_len - m->m_pkthdr.len); 490 } 491#ifdef IPSEC 492 /* 493 * Bypass packet filtering for packets previously handled by IPsec. 494 */ 495 if (ip_ipsec_filtertunnel(m)) 496 goto passin; 497#endif /* IPSEC */ 498 499 /* 500 * Run through list of hooks for input packets. 501 * 502 * NB: Beware of the destination address changing (e.g. 503 * by NAT rewriting). When this happens, tell 504 * ip_forward to do the right thing. 505 */ 506 507 /* Jump over all PFIL processing if hooks are not active. */ 508 if (!PFIL_HOOKED(&V_inet_pfil_hook)) 509 goto passin; 510 511 odst = ip->ip_dst; 512 if (pfil_run_hooks(&V_inet_pfil_hook, &m, ifp, PFIL_IN, NULL) != 0) 513 return; 514 if (m == NULL) /* consumed by filter */ 515 return; 516 517 ip = mtod(m, struct ip *); 518 dchg = (odst.s_addr != ip->ip_dst.s_addr); 519 ifp = m->m_pkthdr.rcvif; 520 521 if (m->m_flags & M_FASTFWD_OURS) { 522 m->m_flags &= ~M_FASTFWD_OURS; 523 goto ours; 524 } 525 if (m->m_flags & M_IP_NEXTHOP) { 526 dchg = (m_tag_find(m, PACKET_TAG_IPFORWARD, NULL) != NULL); 527 if (dchg != 0) { 528 /* 529 * Directly ship the packet on. This allows 530 * forwarding packets originally destined to us 531 * to some other directly connected host. 532 */ 533 ip_forward(m, 1); 534 return; 535 } 536 } 537passin: 538 539 /* 540 * Process options and, if not destined for us, 541 * ship it on. ip_dooptions returns 1 when an 542 * error was detected (causing an icmp message 543 * to be sent and the original packet to be freed). 544 */ 545 if (hlen > sizeof (struct ip) && ip_dooptions(m, 0)) 546 return; 547 548 /* greedy RSVP, snatches any PATH packet of the RSVP protocol and no 549 * matter if it is destined to another node, or whether it is 550 * a multicast one, RSVP wants it! and prevents it from being forwarded 551 * anywhere else. Also checks if the rsvp daemon is running before 552 * grabbing the packet. 553 */ 554 if (V_rsvp_on && ip->ip_p==IPPROTO_RSVP) 555 goto ours; 556 557 /* 558 * Check our list of addresses, to see if the packet is for us. 559 * If we don't have any addresses, assume any unicast packet 560 * we receive might be for us (and let the upper layers deal 561 * with it). 562 */ 563 if (TAILQ_EMPTY(&V_in_ifaddrhead) && 564 (m->m_flags & (M_MCAST|M_BCAST)) == 0) 565 goto ours; 566 567 /* 568 * Enable a consistency check between the destination address 569 * and the arrival interface for a unicast packet (the RFC 1122 570 * strong ES model) if IP forwarding is disabled and the packet 571 * is not locally generated and the packet is not subject to 572 * 'ipfw fwd'. 573 * 574 * XXX - Checking also should be disabled if the destination 575 * address is ipnat'ed to a different interface. 576 * 577 * XXX - Checking is incompatible with IP aliases added 578 * to the loopback interface instead of the interface where 579 * the packets are received. 580 * 581 * XXX - This is the case for carp vhost IPs as well so we 582 * insert a workaround. If the packet got here, we already 583 * checked with carp_iamatch() and carp_forus(). 584 */ 585 checkif = V_ip_checkinterface && (V_ipforwarding == 0) && 586 ifp != NULL && ((ifp->if_flags & IFF_LOOPBACK) == 0) && 587 ifp->if_carp == NULL && (dchg == 0); 588 589 /* 590 * Check for exact addresses in the hash bucket. 591 */ 592 /* IN_IFADDR_RLOCK(); */ 593 LIST_FOREACH(ia, INADDR_HASH(ip->ip_dst.s_addr), ia_hash) { 594 /* 595 * If the address matches, verify that the packet 596 * arrived via the correct interface if checking is 597 * enabled. 598 */ 599 if (IA_SIN(ia)->sin_addr.s_addr == ip->ip_dst.s_addr && 600 (!checkif || ia->ia_ifp == ifp)) { 601 ifa_ref(&ia->ia_ifa); 602 /* IN_IFADDR_RUNLOCK(); */ 603 goto ours; 604 } 605 } 606 /* IN_IFADDR_RUNLOCK(); */ 607 608 /* 609 * Check for broadcast addresses. 610 * 611 * Only accept broadcast packets that arrive via the matching 612 * interface. Reception of forwarded directed broadcasts would 613 * be handled via ip_forward() and ether_output() with the loopback 614 * into the stack for SIMPLEX interfaces handled by ether_output(). 615 */ 616 if (ifp != NULL && ifp->if_flags & IFF_BROADCAST) { 617 IF_ADDR_RLOCK(ifp); 618 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 619 if (ifa->ifa_addr->sa_family != AF_INET) 620 continue; 621 ia = ifatoia(ifa); 622 if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr == 623 ip->ip_dst.s_addr) { 624 ifa_ref(ifa); 625 IF_ADDR_RUNLOCK(ifp); 626 goto ours; 627 } 628#ifdef BOOTP_COMPAT 629 if (IA_SIN(ia)->sin_addr.s_addr == INADDR_ANY) { 630 ifa_ref(ifa); 631 IF_ADDR_RUNLOCK(ifp); 632 goto ours; 633 } 634#endif 635 } 636 IF_ADDR_RUNLOCK(ifp); 637 ia = NULL; 638 } 639 /* RFC 3927 2.7: Do not forward datagrams for 169.254.0.0/16. */ 640 if (IN_LINKLOCAL(ntohl(ip->ip_dst.s_addr))) { 641 IPSTAT_INC(ips_cantforward); 642 m_freem(m); 643 return; 644 } 645 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) { 646 if (V_ip_mrouter) { 647 /* 648 * If we are acting as a multicast router, all 649 * incoming multicast packets are passed to the 650 * kernel-level multicast forwarding function. 651 * The packet is returned (relatively) intact; if 652 * ip_mforward() returns a non-zero value, the packet 653 * must be discarded, else it may be accepted below. 654 */ 655 if (ip_mforward && ip_mforward(ip, ifp, m, 0) != 0) { 656 IPSTAT_INC(ips_cantforward); 657 m_freem(m); 658 return; 659 } 660 661 /* 662 * The process-level routing daemon needs to receive 663 * all multicast IGMP packets, whether or not this 664 * host belongs to their destination groups. 665 */ 666 if (ip->ip_p == IPPROTO_IGMP) 667 goto ours; 668 IPSTAT_INC(ips_forward); 669 } 670 /* 671 * Assume the packet is for us, to avoid prematurely taking 672 * a lock on the in_multi hash. Protocols must perform 673 * their own filtering and update statistics accordingly. 674 */ 675 goto ours; 676 } 677 if (ip->ip_dst.s_addr == (u_long)INADDR_BROADCAST) 678 goto ours; 679 if (ip->ip_dst.s_addr == INADDR_ANY) 680 goto ours; 681 682 /* 683 * FAITH(Firewall Aided Internet Translator) 684 */ 685 if (ifp && ifp->if_type == IFT_FAITH) { 686 if (V_ip_keepfaith) { 687 if (ip->ip_p == IPPROTO_TCP || ip->ip_p == IPPROTO_ICMP) 688 goto ours; 689 } 690 m_freem(m); 691 return; 692 } 693 694 /* 695 * Not for us; forward if possible and desirable. 696 */ 697 if (V_ipforwarding == 0) { 698 IPSTAT_INC(ips_cantforward); 699 m_freem(m); 700 } else { 701#ifdef IPSEC 702 if (ip_ipsec_fwd(m)) 703 goto bad; 704#endif /* IPSEC */ 705 ip_forward(m, dchg); 706 } 707 return; 708 709ours: 710#ifdef IPSTEALTH 711 /* 712 * IPSTEALTH: Process non-routing options only 713 * if the packet is destined for us. 714 */ 715 if (V_ipstealth && hlen > sizeof (struct ip) && ip_dooptions(m, 1)) { 716 if (ia != NULL) 717 ifa_free(&ia->ia_ifa); 718 return; 719 } 720#endif /* IPSTEALTH */ 721 722 /* Count the packet in the ip address stats */ 723 if (ia != NULL) { 724 ia->ia_ifa.if_ipackets++; 725 ia->ia_ifa.if_ibytes += m->m_pkthdr.len; 726 ifa_free(&ia->ia_ifa); 727 } 728 729 /* 730 * Attempt reassembly; if it succeeds, proceed. 731 * ip_reass() will return a different mbuf. 732 */ 733 if (ip->ip_off & htons(IP_MF | IP_OFFMASK)) { 734 m = ip_reass(m); 735 if (m == NULL) 736 return; 737 ip = mtod(m, struct ip *); 738 /* Get the header length of the reassembled packet */ 739 hlen = ip->ip_hl << 2; 740 } 741 742#ifdef IPSEC 743 /* 744 * enforce IPsec policy checking if we are seeing last header. 745 * note that we do not visit this with protocols with pcb layer 746 * code - like udp/tcp/raw ip. 747 */ 748 if (ip_ipsec_input(m)) 749 goto bad; 750#endif /* IPSEC */ 751 752 /* 753 * Switch out to protocol's input routine. 754 */ 755 IPSTAT_INC(ips_delivered); 756 757 (*inetsw[ip_protox[ip->ip_p]].pr_input)(m, hlen); 758 return; 759bad: 760 m_freem(m); 761} 762 763/* 764 * After maxnipq has been updated, propagate the change to UMA. The UMA zone 765 * max has slightly different semantics than the sysctl, for historical 766 * reasons. 767 */ 768static void 769maxnipq_update(void) 770{ 771 772 /* 773 * -1 for unlimited allocation. 774 */ 775 if (V_maxnipq < 0) 776 uma_zone_set_max(V_ipq_zone, 0); 777 /* 778 * Positive number for specific bound. 779 */ 780 if (V_maxnipq > 0) 781 uma_zone_set_max(V_ipq_zone, V_maxnipq); 782 /* 783 * Zero specifies no further fragment queue allocation -- set the 784 * bound very low, but rely on implementation elsewhere to actually 785 * prevent allocation and reclaim current queues. 786 */ 787 if (V_maxnipq == 0) 788 uma_zone_set_max(V_ipq_zone, 1); 789} 790 791static void 792ipq_zone_change(void *tag) 793{ 794 795 if (V_maxnipq > 0 && V_maxnipq < (nmbclusters / 32)) { 796 V_maxnipq = nmbclusters / 32; 797 maxnipq_update(); 798 } 799} 800 801static int 802sysctl_maxnipq(SYSCTL_HANDLER_ARGS) 803{ 804 int error, i; 805 806 i = V_maxnipq; 807 error = sysctl_handle_int(oidp, &i, 0, req); 808 if (error || !req->newptr) 809 return (error); 810 811 /* 812 * XXXRW: Might be a good idea to sanity check the argument and place 813 * an extreme upper bound. 814 */ 815 if (i < -1) 816 return (EINVAL); 817 V_maxnipq = i; 818 maxnipq_update(); 819 return (0); 820} 821 822SYSCTL_PROC(_net_inet_ip, OID_AUTO, maxfragpackets, CTLTYPE_INT|CTLFLAG_RW, 823 NULL, 0, sysctl_maxnipq, "I", 824 "Maximum number of IPv4 fragment reassembly queue entries"); 825 826/* 827 * Take incoming datagram fragment and try to reassemble it into 828 * whole datagram. If the argument is the first fragment or one 829 * in between the function will return NULL and store the mbuf 830 * in the fragment chain. If the argument is the last fragment 831 * the packet will be reassembled and the pointer to the new 832 * mbuf returned for further processing. Only m_tags attached 833 * to the first packet/fragment are preserved. 834 * The IP header is *NOT* adjusted out of iplen. 835 */ 836struct mbuf * 837ip_reass(struct mbuf *m) 838{ 839 struct ip *ip; 840 struct mbuf *p, *q, *nq, *t; 841 struct ipq *fp = NULL; 842 struct ipqhead *head; 843 int i, hlen, next; 844 u_int8_t ecn, ecn0; 845 u_short hash; 846 847 /* If maxnipq or maxfragsperpacket are 0, never accept fragments. */ 848 if (V_maxnipq == 0 || V_maxfragsperpacket == 0) { 849 IPSTAT_INC(ips_fragments); 850 IPSTAT_INC(ips_fragdropped); 851 m_freem(m); 852 return (NULL); 853 } 854 855 ip = mtod(m, struct ip *); 856 hlen = ip->ip_hl << 2; 857 858 hash = IPREASS_HASH(ip->ip_src.s_addr, ip->ip_id); 859 head = &V_ipq[hash]; 860 IPQ_LOCK(); 861 862 /* 863 * Look for queue of fragments 864 * of this datagram. 865 */ 866 TAILQ_FOREACH(fp, head, ipq_list) 867 if (ip->ip_id == fp->ipq_id && 868 ip->ip_src.s_addr == fp->ipq_src.s_addr && 869 ip->ip_dst.s_addr == fp->ipq_dst.s_addr && 870#ifdef MAC 871 mac_ipq_match(m, fp) && 872#endif 873 ip->ip_p == fp->ipq_p) 874 goto found; 875 876 fp = NULL; 877 878 /* 879 * Attempt to trim the number of allocated fragment queues if it 880 * exceeds the administrative limit. 881 */ 882 if ((V_nipq > V_maxnipq) && (V_maxnipq > 0)) { 883 /* 884 * drop something from the tail of the current queue 885 * before proceeding further 886 */ 887 struct ipq *q = TAILQ_LAST(head, ipqhead); 888 if (q == NULL) { /* gak */ 889 for (i = 0; i < IPREASS_NHASH; i++) { 890 struct ipq *r = TAILQ_LAST(&V_ipq[i], ipqhead); 891 if (r) { 892 IPSTAT_ADD(ips_fragtimeout, 893 r->ipq_nfrags); 894 ip_freef(&V_ipq[i], r); 895 break; 896 } 897 } 898 } else { 899 IPSTAT_ADD(ips_fragtimeout, q->ipq_nfrags); 900 ip_freef(head, q); 901 } 902 } 903 904found: 905 /* 906 * Adjust ip_len to not reflect header, 907 * convert offset of this to bytes. 908 */ 909 ip->ip_len = htons(ntohs(ip->ip_len) - hlen); 910 if (ip->ip_off & htons(IP_MF)) { 911 /* 912 * Make sure that fragments have a data length 913 * that's a non-zero multiple of 8 bytes. 914 */ 915 if (ip->ip_len == htons(0) || (ntohs(ip->ip_len) & 0x7) != 0) { 916 IPSTAT_INC(ips_toosmall); /* XXX */ 917 goto dropfrag; 918 } 919 m->m_flags |= M_IP_FRAG; 920 } else 921 m->m_flags &= ~M_IP_FRAG; 922 ip->ip_off = htons(ntohs(ip->ip_off) << 3); 923 924 /* 925 * Attempt reassembly; if it succeeds, proceed. 926 * ip_reass() will return a different mbuf. 927 */ 928 IPSTAT_INC(ips_fragments); 929 m->m_pkthdr.PH_loc.ptr = ip; 930 931 /* Previous ip_reass() started here. */ 932 /* 933 * Presence of header sizes in mbufs 934 * would confuse code below. 935 */ 936 m->m_data += hlen; 937 m->m_len -= hlen; 938 939 /* 940 * If first fragment to arrive, create a reassembly queue. 941 */ 942 if (fp == NULL) { 943 fp = uma_zalloc(V_ipq_zone, M_NOWAIT); 944 if (fp == NULL) 945 goto dropfrag; 946#ifdef MAC 947 if (mac_ipq_init(fp, M_NOWAIT) != 0) { 948 uma_zfree(V_ipq_zone, fp); 949 fp = NULL; 950 goto dropfrag; 951 } 952 mac_ipq_create(m, fp); 953#endif 954 TAILQ_INSERT_HEAD(head, fp, ipq_list); 955 V_nipq++; 956 fp->ipq_nfrags = 1; 957 fp->ipq_ttl = IPFRAGTTL; 958 fp->ipq_p = ip->ip_p; 959 fp->ipq_id = ip->ip_id; 960 fp->ipq_src = ip->ip_src; 961 fp->ipq_dst = ip->ip_dst; 962 fp->ipq_frags = m; 963 m->m_nextpkt = NULL; 964 goto done; 965 } else { 966 fp->ipq_nfrags++; 967#ifdef MAC 968 mac_ipq_update(m, fp); 969#endif 970 } 971 972#define GETIP(m) ((struct ip*)((m)->m_pkthdr.PH_loc.ptr)) 973 974 /* 975 * Handle ECN by comparing this segment with the first one; 976 * if CE is set, do not lose CE. 977 * drop if CE and not-ECT are mixed for the same packet. 978 */ 979 ecn = ip->ip_tos & IPTOS_ECN_MASK; 980 ecn0 = GETIP(fp->ipq_frags)->ip_tos & IPTOS_ECN_MASK; 981 if (ecn == IPTOS_ECN_CE) { 982 if (ecn0 == IPTOS_ECN_NOTECT) 983 goto dropfrag; 984 if (ecn0 != IPTOS_ECN_CE) 985 GETIP(fp->ipq_frags)->ip_tos |= IPTOS_ECN_CE; 986 } 987 if (ecn == IPTOS_ECN_NOTECT && ecn0 != IPTOS_ECN_NOTECT) 988 goto dropfrag; 989 990 /* 991 * Find a segment which begins after this one does. 992 */ 993 for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt) 994 if (ntohs(GETIP(q)->ip_off) > ntohs(ip->ip_off)) 995 break; 996 997 /* 998 * If there is a preceding segment, it may provide some of 999 * our data already. If so, drop the data from the incoming 1000 * segment. If it provides all of our data, drop us, otherwise 1001 * stick new segment in the proper place. 1002 * 1003 * If some of the data is dropped from the preceding 1004 * segment, then it's checksum is invalidated. 1005 */ 1006 if (p) { 1007 i = ntohs(GETIP(p)->ip_off) + ntohs(GETIP(p)->ip_len) - 1008 ntohs(ip->ip_off); 1009 if (i > 0) { 1010 if (i >= ntohs(ip->ip_len)) 1011 goto dropfrag; 1012 m_adj(m, i); 1013 m->m_pkthdr.csum_flags = 0; 1014 ip->ip_off = htons(ntohs(ip->ip_off) + i); 1015 ip->ip_len = htons(ntohs(ip->ip_len) - i); 1016 } 1017 m->m_nextpkt = p->m_nextpkt; 1018 p->m_nextpkt = m; 1019 } else { 1020 m->m_nextpkt = fp->ipq_frags; 1021 fp->ipq_frags = m; 1022 } 1023 1024 /* 1025 * While we overlap succeeding segments trim them or, 1026 * if they are completely covered, dequeue them. 1027 */ 1028 for (; q != NULL && ntohs(ip->ip_off) + ntohs(ip->ip_len) > 1029 ntohs(GETIP(q)->ip_off); q = nq) { 1030 i = (ntohs(ip->ip_off) + ntohs(ip->ip_len)) - 1031 ntohs(GETIP(q)->ip_off); 1032 if (i < ntohs(GETIP(q)->ip_len)) { 1033 GETIP(q)->ip_len = htons(ntohs(GETIP(q)->ip_len) - i); 1034 GETIP(q)->ip_off = htons(ntohs(GETIP(q)->ip_off) + i); 1035 m_adj(q, i); 1036 q->m_pkthdr.csum_flags = 0; 1037 break; 1038 } 1039 nq = q->m_nextpkt; 1040 m->m_nextpkt = nq; 1041 IPSTAT_INC(ips_fragdropped); 1042 fp->ipq_nfrags--; 1043 m_freem(q); 1044 } 1045 1046 /* 1047 * Check for complete reassembly and perform frag per packet 1048 * limiting. 1049 * 1050 * Frag limiting is performed here so that the nth frag has 1051 * a chance to complete the packet before we drop the packet. 1052 * As a result, n+1 frags are actually allowed per packet, but 1053 * only n will ever be stored. (n = maxfragsperpacket.) 1054 * 1055 */ 1056 next = 0; 1057 for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt) { 1058 if (ntohs(GETIP(q)->ip_off) != next) { 1059 if (fp->ipq_nfrags > V_maxfragsperpacket) { 1060 IPSTAT_ADD(ips_fragdropped, fp->ipq_nfrags); 1061 ip_freef(head, fp); 1062 } 1063 goto done; 1064 } 1065 next += ntohs(GETIP(q)->ip_len); 1066 } 1067 /* Make sure the last packet didn't have the IP_MF flag */ 1068 if (p->m_flags & M_IP_FRAG) { 1069 if (fp->ipq_nfrags > V_maxfragsperpacket) { 1070 IPSTAT_ADD(ips_fragdropped, fp->ipq_nfrags); 1071 ip_freef(head, fp); 1072 } 1073 goto done; 1074 } 1075 1076 /* 1077 * Reassembly is complete. Make sure the packet is a sane size. 1078 */ 1079 q = fp->ipq_frags; 1080 ip = GETIP(q); 1081 if (next + (ip->ip_hl << 2) > IP_MAXPACKET) { 1082 IPSTAT_INC(ips_toolong); 1083 IPSTAT_ADD(ips_fragdropped, fp->ipq_nfrags); 1084 ip_freef(head, fp); 1085 goto done; 1086 } 1087 1088 /* 1089 * Concatenate fragments. 1090 */ 1091 m = q; 1092 t = m->m_next; 1093 m->m_next = NULL; 1094 m_cat(m, t); 1095 nq = q->m_nextpkt; 1096 q->m_nextpkt = NULL; 1097 for (q = nq; q != NULL; q = nq) { 1098 nq = q->m_nextpkt; 1099 q->m_nextpkt = NULL; 1100 m->m_pkthdr.csum_flags &= q->m_pkthdr.csum_flags; 1101 m->m_pkthdr.csum_data += q->m_pkthdr.csum_data; 1102 m_cat(m, q); 1103 } 1104 /* 1105 * In order to do checksumming faster we do 'end-around carry' here 1106 * (and not in for{} loop), though it implies we are not going to 1107 * reassemble more than 64k fragments. 1108 */ 1109 m->m_pkthdr.csum_data = 1110 (m->m_pkthdr.csum_data & 0xffff) + (m->m_pkthdr.csum_data >> 16); 1111#ifdef MAC 1112 mac_ipq_reassemble(fp, m); 1113 mac_ipq_destroy(fp); 1114#endif 1115 1116 /* 1117 * Create header for new ip packet by modifying header of first 1118 * packet; dequeue and discard fragment reassembly header. 1119 * Make header visible. 1120 */ 1121 ip->ip_len = htons((ip->ip_hl << 2) + next); 1122 ip->ip_src = fp->ipq_src; 1123 ip->ip_dst = fp->ipq_dst; 1124 TAILQ_REMOVE(head, fp, ipq_list); 1125 V_nipq--; 1126 uma_zfree(V_ipq_zone, fp); 1127 m->m_len += (ip->ip_hl << 2); 1128 m->m_data -= (ip->ip_hl << 2); 1129 /* some debugging cruft by sklower, below, will go away soon */ 1130 if (m->m_flags & M_PKTHDR) /* XXX this should be done elsewhere */ 1131 m_fixhdr(m); 1132 IPSTAT_INC(ips_reassembled); 1133 IPQ_UNLOCK(); 1134 return (m); 1135 1136dropfrag: 1137 IPSTAT_INC(ips_fragdropped); 1138 if (fp != NULL) 1139 fp->ipq_nfrags--; 1140 m_freem(m); 1141done: 1142 IPQ_UNLOCK(); 1143 return (NULL); 1144 1145#undef GETIP 1146} 1147 1148/* 1149 * Free a fragment reassembly header and all 1150 * associated datagrams. 1151 */ 1152static void 1153ip_freef(struct ipqhead *fhp, struct ipq *fp) 1154{ 1155 struct mbuf *q; 1156 1157 IPQ_LOCK_ASSERT(); 1158 1159 while (fp->ipq_frags) { 1160 q = fp->ipq_frags; 1161 fp->ipq_frags = q->m_nextpkt; 1162 m_freem(q); 1163 } 1164 TAILQ_REMOVE(fhp, fp, ipq_list); 1165 uma_zfree(V_ipq_zone, fp); 1166 V_nipq--; 1167} 1168 1169/* 1170 * IP timer processing; 1171 * if a timer expires on a reassembly 1172 * queue, discard it. 1173 */ 1174void 1175ip_slowtimo(void) 1176{ 1177 VNET_ITERATOR_DECL(vnet_iter); 1178 struct ipq *fp; 1179 int i; 1180 1181 VNET_LIST_RLOCK_NOSLEEP(); 1182 IPQ_LOCK(); 1183 VNET_FOREACH(vnet_iter) { 1184 CURVNET_SET(vnet_iter); 1185 for (i = 0; i < IPREASS_NHASH; i++) { 1186 for(fp = TAILQ_FIRST(&V_ipq[i]); fp;) { 1187 struct ipq *fpp; 1188 1189 fpp = fp; 1190 fp = TAILQ_NEXT(fp, ipq_list); 1191 if(--fpp->ipq_ttl == 0) { 1192 IPSTAT_ADD(ips_fragtimeout, 1193 fpp->ipq_nfrags); 1194 ip_freef(&V_ipq[i], fpp); 1195 } 1196 } 1197 } 1198 /* 1199 * If we are over the maximum number of fragments 1200 * (due to the limit being lowered), drain off 1201 * enough to get down to the new limit. 1202 */ 1203 if (V_maxnipq >= 0 && V_nipq > V_maxnipq) { 1204 for (i = 0; i < IPREASS_NHASH; i++) { 1205 while (V_nipq > V_maxnipq && 1206 !TAILQ_EMPTY(&V_ipq[i])) { 1207 IPSTAT_ADD(ips_fragdropped, 1208 TAILQ_FIRST(&V_ipq[i])->ipq_nfrags); 1209 ip_freef(&V_ipq[i], 1210 TAILQ_FIRST(&V_ipq[i])); 1211 } 1212 } 1213 } 1214 CURVNET_RESTORE(); 1215 } 1216 IPQ_UNLOCK(); 1217 VNET_LIST_RUNLOCK_NOSLEEP(); 1218} 1219 1220/* 1221 * Drain off all datagram fragments. 1222 */ 1223static void 1224ip_drain_locked(void) 1225{ 1226 int i; 1227 1228 IPQ_LOCK_ASSERT(); 1229 1230 for (i = 0; i < IPREASS_NHASH; i++) { 1231 while(!TAILQ_EMPTY(&V_ipq[i])) { 1232 IPSTAT_ADD(ips_fragdropped, 1233 TAILQ_FIRST(&V_ipq[i])->ipq_nfrags); 1234 ip_freef(&V_ipq[i], TAILQ_FIRST(&V_ipq[i])); 1235 } 1236 } 1237} 1238 1239void 1240ip_drain(void) 1241{ 1242 VNET_ITERATOR_DECL(vnet_iter); 1243 1244 VNET_LIST_RLOCK_NOSLEEP(); 1245 IPQ_LOCK(); 1246 VNET_FOREACH(vnet_iter) { 1247 CURVNET_SET(vnet_iter); 1248 ip_drain_locked(); 1249 CURVNET_RESTORE(); 1250 } 1251 IPQ_UNLOCK(); 1252 VNET_LIST_RUNLOCK_NOSLEEP(); 1253 in_rtqdrain(); 1254} 1255 1256/* 1257 * The protocol to be inserted into ip_protox[] must be already registered 1258 * in inetsw[], either statically or through pf_proto_register(). 1259 */ 1260int 1261ipproto_register(short ipproto) 1262{ 1263 struct protosw *pr; 1264 1265 /* Sanity checks. */ 1266 if (ipproto <= 0 || ipproto >= IPPROTO_MAX) 1267 return (EPROTONOSUPPORT); 1268 1269 /* 1270 * The protocol slot must not be occupied by another protocol 1271 * already. An index pointing to IPPROTO_RAW is unused. 1272 */ 1273 pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW); 1274 if (pr == NULL) 1275 return (EPFNOSUPPORT); 1276 if (ip_protox[ipproto] != pr - inetsw) /* IPPROTO_RAW */ 1277 return (EEXIST); 1278 1279 /* Find the protocol position in inetsw[] and set the index. */ 1280 for (pr = inetdomain.dom_protosw; 1281 pr < inetdomain.dom_protoswNPROTOSW; pr++) { 1282 if (pr->pr_domain->dom_family == PF_INET && 1283 pr->pr_protocol && pr->pr_protocol == ipproto) { 1284 ip_protox[pr->pr_protocol] = pr - inetsw; 1285 return (0); 1286 } 1287 } 1288 return (EPROTONOSUPPORT); 1289} 1290 1291int 1292ipproto_unregister(short ipproto) 1293{ 1294 struct protosw *pr; 1295 1296 /* Sanity checks. */ 1297 if (ipproto <= 0 || ipproto >= IPPROTO_MAX) 1298 return (EPROTONOSUPPORT); 1299 1300 /* Check if the protocol was indeed registered. */ 1301 pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW); 1302 if (pr == NULL) 1303 return (EPFNOSUPPORT); 1304 if (ip_protox[ipproto] == pr - inetsw) /* IPPROTO_RAW */ 1305 return (ENOENT); 1306 1307 /* Reset the protocol slot to IPPROTO_RAW. */ 1308 ip_protox[ipproto] = pr - inetsw; 1309 return (0); 1310} 1311 1312/* 1313 * Given address of next destination (final or next hop), return (referenced) 1314 * internet address info of interface to be used to get there. 1315 */ 1316struct in_ifaddr * 1317ip_rtaddr(struct in_addr dst, u_int fibnum) 1318{ 1319 struct route sro; 1320 struct sockaddr_in *sin; 1321 struct in_ifaddr *ia; 1322 1323 bzero(&sro, sizeof(sro)); 1324 sin = (struct sockaddr_in *)&sro.ro_dst; 1325 sin->sin_family = AF_INET; 1326 sin->sin_len = sizeof(*sin); 1327 sin->sin_addr = dst; 1328 in_rtalloc_ign(&sro, 0, fibnum); 1329 1330 if (sro.ro_rt == NULL) 1331 return (NULL); 1332 1333 ia = ifatoia(sro.ro_rt->rt_ifa); 1334 ifa_ref(&ia->ia_ifa); 1335 RTFREE(sro.ro_rt); 1336 return (ia); 1337} 1338 1339u_char inetctlerrmap[PRC_NCMDS] = { 1340 0, 0, 0, 0, 1341 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH, 1342 EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED, 1343 EMSGSIZE, EHOSTUNREACH, 0, 0, 1344 0, 0, EHOSTUNREACH, 0, 1345 ENOPROTOOPT, ECONNREFUSED 1346}; 1347 1348/* 1349 * Forward a packet. If some error occurs return the sender 1350 * an icmp packet. Note we can't always generate a meaningful 1351 * icmp message because icmp doesn't have a large enough repertoire 1352 * of codes and types. 1353 * 1354 * If not forwarding, just drop the packet. This could be confusing 1355 * if ipforwarding was zero but some routing protocol was advancing 1356 * us as a gateway to somewhere. However, we must let the routing 1357 * protocol deal with that. 1358 * 1359 * The srcrt parameter indicates whether the packet is being forwarded 1360 * via a source route. 1361 */ 1362void 1363ip_forward(struct mbuf *m, int srcrt) 1364{ 1365 struct ip *ip = mtod(m, struct ip *); 1366 struct in_ifaddr *ia; 1367 struct mbuf *mcopy; 1368 struct in_addr dest; 1369 struct route ro; 1370 int error, type = 0, code = 0, mtu = 0; 1371 1372 if (m->m_flags & (M_BCAST|M_MCAST) || in_canforward(ip->ip_dst) == 0) { 1373 IPSTAT_INC(ips_cantforward); 1374 m_freem(m); 1375 return; 1376 } 1377#ifdef IPSTEALTH 1378 if (!V_ipstealth) { 1379#endif 1380 if (ip->ip_ttl <= IPTTLDEC) { 1381 icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, 1382 0, 0); 1383 return; 1384 } 1385#ifdef IPSTEALTH 1386 } 1387#endif 1388 1389 ia = ip_rtaddr(ip->ip_dst, M_GETFIB(m)); 1390#ifndef IPSEC 1391 /* 1392 * 'ia' may be NULL if there is no route for this destination. 1393 * In case of IPsec, Don't discard it just yet, but pass it to 1394 * ip_output in case of outgoing IPsec policy. 1395 */ 1396 if (!srcrt && ia == NULL) { 1397 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0); 1398 return; 1399 } 1400#endif 1401 1402 /* 1403 * Save the IP header and at most 8 bytes of the payload, 1404 * in case we need to generate an ICMP message to the src. 1405 * 1406 * XXX this can be optimized a lot by saving the data in a local 1407 * buffer on the stack (72 bytes at most), and only allocating the 1408 * mbuf if really necessary. The vast majority of the packets 1409 * are forwarded without having to send an ICMP back (either 1410 * because unnecessary, or because rate limited), so we are 1411 * really we are wasting a lot of work here. 1412 * 1413 * We don't use m_copy() because it might return a reference 1414 * to a shared cluster. Both this function and ip_output() 1415 * assume exclusive access to the IP header in `m', so any 1416 * data in a cluster may change before we reach icmp_error(). 1417 */ 1418 mcopy = m_gethdr(M_NOWAIT, m->m_type); 1419 if (mcopy != NULL && !m_dup_pkthdr(mcopy, m, M_NOWAIT)) { 1420 /* 1421 * It's probably ok if the pkthdr dup fails (because 1422 * the deep copy of the tag chain failed), but for now 1423 * be conservative and just discard the copy since 1424 * code below may some day want the tags. 1425 */ 1426 m_free(mcopy); 1427 mcopy = NULL; 1428 } 1429 if (mcopy != NULL) { 1430 mcopy->m_len = min(ntohs(ip->ip_len), M_TRAILINGSPACE(mcopy)); 1431 mcopy->m_pkthdr.len = mcopy->m_len; 1432 m_copydata(m, 0, mcopy->m_len, mtod(mcopy, caddr_t)); 1433 } 1434 1435#ifdef IPSTEALTH 1436 if (!V_ipstealth) { 1437#endif 1438 ip->ip_ttl -= IPTTLDEC; 1439#ifdef IPSTEALTH 1440 } 1441#endif 1442 1443 /* 1444 * If forwarding packet using same interface that it came in on, 1445 * perhaps should send a redirect to sender to shortcut a hop. 1446 * Only send redirect if source is sending directly to us, 1447 * and if packet was not source routed (or has any options). 1448 * Also, don't send redirect if forwarding using a default route 1449 * or a route modified by a redirect. 1450 */ 1451 dest.s_addr = 0; 1452 if (!srcrt && V_ipsendredirects && 1453 ia != NULL && ia->ia_ifp == m->m_pkthdr.rcvif) { 1454 struct sockaddr_in *sin; 1455 struct rtentry *rt; 1456 1457 bzero(&ro, sizeof(ro)); 1458 sin = (struct sockaddr_in *)&ro.ro_dst; 1459 sin->sin_family = AF_INET; 1460 sin->sin_len = sizeof(*sin); 1461 sin->sin_addr = ip->ip_dst; 1462 in_rtalloc_ign(&ro, 0, M_GETFIB(m)); 1463 1464 rt = ro.ro_rt; 1465 1466 if (rt && (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 && 1467 satosin(rt_key(rt))->sin_addr.s_addr != 0) { 1468#define RTA(rt) ((struct in_ifaddr *)(rt->rt_ifa)) 1469 u_long src = ntohl(ip->ip_src.s_addr); 1470 1471 if (RTA(rt) && 1472 (src & RTA(rt)->ia_subnetmask) == RTA(rt)->ia_subnet) { 1473 if (rt->rt_flags & RTF_GATEWAY) 1474 dest.s_addr = satosin(rt->rt_gateway)->sin_addr.s_addr; 1475 else 1476 dest.s_addr = ip->ip_dst.s_addr; 1477 /* Router requirements says to only send host redirects */ 1478 type = ICMP_REDIRECT; 1479 code = ICMP_REDIRECT_HOST; 1480 } 1481 } 1482 if (rt) 1483 RTFREE(rt); 1484 } 1485 1486 /* 1487 * Try to cache the route MTU from ip_output so we can consider it for 1488 * the ICMP_UNREACH_NEEDFRAG "Next-Hop MTU" field described in RFC1191. 1489 */ 1490 bzero(&ro, sizeof(ro)); 1491 1492 error = ip_output(m, NULL, &ro, IP_FORWARDING, NULL, NULL); 1493 1494 if (error == EMSGSIZE && ro.ro_rt) 1495 mtu = ro.ro_rt->rt_rmx.rmx_mtu; 1496 RO_RTFREE(&ro); 1497 1498 if (error) 1499 IPSTAT_INC(ips_cantforward); 1500 else { 1501 IPSTAT_INC(ips_forward); 1502 if (type) 1503 IPSTAT_INC(ips_redirectsent); 1504 else { 1505 if (mcopy) 1506 m_freem(mcopy); 1507 if (ia != NULL) 1508 ifa_free(&ia->ia_ifa); 1509 return; 1510 } 1511 } 1512 if (mcopy == NULL) { 1513 if (ia != NULL) 1514 ifa_free(&ia->ia_ifa); 1515 return; 1516 } 1517 1518 switch (error) { 1519 1520 case 0: /* forwarded, but need redirect */ 1521 /* type, code set above */ 1522 break; 1523 1524 case ENETUNREACH: 1525 case EHOSTUNREACH: 1526 case ENETDOWN: 1527 case EHOSTDOWN: 1528 default: 1529 type = ICMP_UNREACH; 1530 code = ICMP_UNREACH_HOST; 1531 break; 1532 1533 case EMSGSIZE: 1534 type = ICMP_UNREACH; 1535 code = ICMP_UNREACH_NEEDFRAG; 1536 1537#ifdef IPSEC 1538 /* 1539 * If IPsec is configured for this path, 1540 * override any possibly mtu value set by ip_output. 1541 */ 1542 mtu = ip_ipsec_mtu(mcopy, mtu); 1543#endif /* IPSEC */ 1544 /* 1545 * If the MTU was set before make sure we are below the 1546 * interface MTU. 1547 * If the MTU wasn't set before use the interface mtu or 1548 * fall back to the next smaller mtu step compared to the 1549 * current packet size. 1550 */ 1551 if (mtu != 0) { 1552 if (ia != NULL) 1553 mtu = min(mtu, ia->ia_ifp->if_mtu); 1554 } else { 1555 if (ia != NULL) 1556 mtu = ia->ia_ifp->if_mtu; 1557 else 1558 mtu = ip_next_mtu(ntohs(ip->ip_len), 0); 1559 } 1560 IPSTAT_INC(ips_cantfrag); 1561 break; 1562 1563 case ENOBUFS: 1564 /* 1565 * A router should not generate ICMP_SOURCEQUENCH as 1566 * required in RFC1812 Requirements for IP Version 4 Routers. 1567 * Source quench could be a big problem under DoS attacks, 1568 * or if the underlying interface is rate-limited. 1569 * Those who need source quench packets may re-enable them 1570 * via the net.inet.ip.sendsourcequench sysctl. 1571 */ 1572 if (V_ip_sendsourcequench == 0) { 1573 m_freem(mcopy); 1574 if (ia != NULL) 1575 ifa_free(&ia->ia_ifa); 1576 return; 1577 } else { 1578 type = ICMP_SOURCEQUENCH; 1579 code = 0; 1580 } 1581 break; 1582 1583 case EACCES: /* ipfw denied packet */ 1584 m_freem(mcopy); 1585 if (ia != NULL) 1586 ifa_free(&ia->ia_ifa); 1587 return; 1588 } 1589 if (ia != NULL) 1590 ifa_free(&ia->ia_ifa); 1591 icmp_error(mcopy, type, code, dest.s_addr, mtu); 1592} 1593 1594void 1595ip_savecontrol(struct inpcb *inp, struct mbuf **mp, struct ip *ip, 1596 struct mbuf *m) 1597{ 1598 1599 if (inp->inp_socket->so_options & (SO_BINTIME | SO_TIMESTAMP)) { 1600 struct bintime bt; 1601 1602 bintime(&bt); 1603 if (inp->inp_socket->so_options & SO_BINTIME) { 1604 *mp = sbcreatecontrol((caddr_t)&bt, sizeof(bt), 1605 SCM_BINTIME, SOL_SOCKET); 1606 if (*mp) 1607 mp = &(*mp)->m_next; 1608 } 1609 if (inp->inp_socket->so_options & SO_TIMESTAMP) { 1610 struct timeval tv; 1611 1612 bintime2timeval(&bt, &tv); 1613 *mp = sbcreatecontrol((caddr_t)&tv, sizeof(tv), 1614 SCM_TIMESTAMP, SOL_SOCKET); 1615 if (*mp) 1616 mp = &(*mp)->m_next; 1617 } 1618 } 1619 if (inp->inp_flags & INP_RECVDSTADDR) { 1620 *mp = sbcreatecontrol((caddr_t)&ip->ip_dst, 1621 sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP); 1622 if (*mp) 1623 mp = &(*mp)->m_next; 1624 } 1625 if (inp->inp_flags & INP_RECVTTL) { 1626 *mp = sbcreatecontrol((caddr_t)&ip->ip_ttl, 1627 sizeof(u_char), IP_RECVTTL, IPPROTO_IP); 1628 if (*mp) 1629 mp = &(*mp)->m_next; 1630 } 1631#ifdef notyet 1632 /* XXX 1633 * Moving these out of udp_input() made them even more broken 1634 * than they already were. 1635 */ 1636 /* options were tossed already */ 1637 if (inp->inp_flags & INP_RECVOPTS) { 1638 *mp = sbcreatecontrol((caddr_t)opts_deleted_above, 1639 sizeof(struct in_addr), IP_RECVOPTS, IPPROTO_IP); 1640 if (*mp) 1641 mp = &(*mp)->m_next; 1642 } 1643 /* ip_srcroute doesn't do what we want here, need to fix */ 1644 if (inp->inp_flags & INP_RECVRETOPTS) { 1645 *mp = sbcreatecontrol((caddr_t)ip_srcroute(m), 1646 sizeof(struct in_addr), IP_RECVRETOPTS, IPPROTO_IP); 1647 if (*mp) 1648 mp = &(*mp)->m_next; 1649 } 1650#endif 1651 if (inp->inp_flags & INP_RECVIF) { 1652 struct ifnet *ifp; 1653 struct sdlbuf { 1654 struct sockaddr_dl sdl; 1655 u_char pad[32]; 1656 } sdlbuf; 1657 struct sockaddr_dl *sdp; 1658 struct sockaddr_dl *sdl2 = &sdlbuf.sdl; 1659 1660 if ((ifp = m->m_pkthdr.rcvif) && 1661 ifp->if_index && ifp->if_index <= V_if_index) { 1662 sdp = (struct sockaddr_dl *)ifp->if_addr->ifa_addr; 1663 /* 1664 * Change our mind and don't try copy. 1665 */ 1666 if (sdp->sdl_family != AF_LINK || 1667 sdp->sdl_len > sizeof(sdlbuf)) { 1668 goto makedummy; 1669 } 1670 bcopy(sdp, sdl2, sdp->sdl_len); 1671 } else { 1672makedummy: 1673 sdl2->sdl_len = 1674 offsetof(struct sockaddr_dl, sdl_data[0]); 1675 sdl2->sdl_family = AF_LINK; 1676 sdl2->sdl_index = 0; 1677 sdl2->sdl_nlen = sdl2->sdl_alen = sdl2->sdl_slen = 0; 1678 } 1679 *mp = sbcreatecontrol((caddr_t)sdl2, sdl2->sdl_len, 1680 IP_RECVIF, IPPROTO_IP); 1681 if (*mp) 1682 mp = &(*mp)->m_next; 1683 } 1684 if (inp->inp_flags & INP_RECVTOS) { 1685 *mp = sbcreatecontrol((caddr_t)&ip->ip_tos, 1686 sizeof(u_char), IP_RECVTOS, IPPROTO_IP); 1687 if (*mp) 1688 mp = &(*mp)->m_next; 1689 } 1690} 1691 1692/* 1693 * XXXRW: Multicast routing code in ip_mroute.c is generally MPSAFE, but the 1694 * ip_rsvp and ip_rsvp_on variables need to be interlocked with rsvp_on 1695 * locking. This code remains in ip_input.c as ip_mroute.c is optionally 1696 * compiled. 1697 */ 1698static VNET_DEFINE(int, ip_rsvp_on); 1699VNET_DEFINE(struct socket *, ip_rsvpd); 1700 1701#define V_ip_rsvp_on VNET(ip_rsvp_on) 1702 1703int 1704ip_rsvp_init(struct socket *so) 1705{ 1706 1707 if (so->so_type != SOCK_RAW || 1708 so->so_proto->pr_protocol != IPPROTO_RSVP) 1709 return EOPNOTSUPP; 1710 1711 if (V_ip_rsvpd != NULL) 1712 return EADDRINUSE; 1713 1714 V_ip_rsvpd = so; 1715 /* 1716 * This may seem silly, but we need to be sure we don't over-increment 1717 * the RSVP counter, in case something slips up. 1718 */ 1719 if (!V_ip_rsvp_on) { 1720 V_ip_rsvp_on = 1; 1721 V_rsvp_on++; 1722 } 1723 1724 return 0; 1725} 1726 1727int 1728ip_rsvp_done(void) 1729{ 1730 1731 V_ip_rsvpd = NULL; 1732 /* 1733 * This may seem silly, but we need to be sure we don't over-decrement 1734 * the RSVP counter, in case something slips up. 1735 */ 1736 if (V_ip_rsvp_on) { 1737 V_ip_rsvp_on = 0; 1738 V_rsvp_on--; 1739 } 1740 return 0; 1741} 1742 1743void 1744rsvp_input(struct mbuf *m, int off) /* XXX must fixup manually */ 1745{ 1746 1747 if (rsvp_input_p) { /* call the real one if loaded */ 1748 rsvp_input_p(m, off); 1749 return; 1750 } 1751 1752 /* Can still get packets with rsvp_on = 0 if there is a local member 1753 * of the group to which the RSVP packet is addressed. But in this 1754 * case we want to throw the packet away. 1755 */ 1756 1757 if (!V_rsvp_on) { 1758 m_freem(m); 1759 return; 1760 } 1761 1762 if (V_ip_rsvpd != NULL) { 1763 rip_input(m, off); 1764 return; 1765 } 1766 /* Drop the packet */ 1767 m_freem(m); 1768}
|