1/* 2 * Copyright (c) 1982, 1986, 1988, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * @(#)raw_ip.c 8.7 (Berkeley) 5/15/95
| 1/* 2 * Copyright (c) 1982, 1986, 1988, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * @(#)raw_ip.c 8.7 (Berkeley) 5/15/95
|
34 * $FreeBSD: head/sys/netinet/raw_ip.c 54799 1999-12-19 01:55:37Z green $
| 34 * $FreeBSD: head/sys/netinet/raw_ip.c 55009 1999-12-22 19:13:38Z shin $
|
35 */ 36
| 35 */ 36
|
| 37#include "opt_inet6.h" 38#include "opt_ipsec.h" 39
|
37#include <sys/param.h> 38#include <sys/systm.h> 39#include <sys/kernel.h> 40#include <sys/malloc.h> 41#include <sys/mbuf.h> 42#include <sys/proc.h> 43#include <sys/protosw.h> 44#include <sys/socket.h> 45#include <sys/socketvar.h> 46#include <sys/sysctl.h> 47 48#include <vm/vm_zone.h> 49 50#include <net/if.h> 51#include <net/route.h> 52 53#define _IP_VHL 54#include <netinet/in.h> 55#include <netinet/in_systm.h> 56#include <netinet/ip.h> 57#include <netinet/in_pcb.h> 58#include <netinet/in_var.h> 59#include <netinet/ip_var.h> 60#include <netinet/ip_mroute.h> 61 62#include <netinet/ip_fw.h> 63
| 40#include <sys/param.h> 41#include <sys/systm.h> 42#include <sys/kernel.h> 43#include <sys/malloc.h> 44#include <sys/mbuf.h> 45#include <sys/proc.h> 46#include <sys/protosw.h> 47#include <sys/socket.h> 48#include <sys/socketvar.h> 49#include <sys/sysctl.h> 50 51#include <vm/vm_zone.h> 52 53#include <net/if.h> 54#include <net/route.h> 55 56#define _IP_VHL 57#include <netinet/in.h> 58#include <netinet/in_systm.h> 59#include <netinet/ip.h> 60#include <netinet/in_pcb.h> 61#include <netinet/in_var.h> 62#include <netinet/ip_var.h> 63#include <netinet/ip_mroute.h> 64 65#include <netinet/ip_fw.h> 66
|
| 67#ifdef IPSEC 68#include <netinet6/ipsec.h> 69#endif /*IPSEC*/ 70
|
64#include "opt_ipdn.h" 65#ifdef DUMMYNET 66#include <netinet/ip_dummynet.h> 67#endif 68 69struct inpcbhead ripcb; 70struct inpcbinfo ripcbinfo; 71 72/* 73 * Nominal space allocated to a raw ip socket. 74 */ 75#define RIPSNDQ 8192 76#define RIPRCVQ 8192 77 78/* 79 * Raw interface to IP protocol. 80 */ 81 82/* 83 * Initialize raw connection block q. 84 */ 85void 86rip_init() 87{ 88 LIST_INIT(&ripcb); 89 ripcbinfo.listhead = &ripcb; 90 /* 91 * XXX We don't use the hash list for raw IP, but it's easier 92 * to allocate a one entry hash list than it is to check all 93 * over the place for hashbase == NULL. 94 */ 95 ripcbinfo.hashbase = hashinit(1, M_PCB, &ripcbinfo.hashmask); 96 ripcbinfo.porthashbase = hashinit(1, M_PCB, &ripcbinfo.porthashmask); 97 ripcbinfo.ipi_zone = zinit("ripcb", sizeof(struct inpcb), 98 maxsockets, ZONE_INTERRUPT, 0); 99} 100 101static struct sockaddr_in ripsrc = { sizeof(ripsrc), AF_INET }; 102/* 103 * Setup generic address and protocol structures 104 * for raw_input routine, then pass them along with 105 * mbuf chain. 106 */ 107void
| 71#include "opt_ipdn.h" 72#ifdef DUMMYNET 73#include <netinet/ip_dummynet.h> 74#endif 75 76struct inpcbhead ripcb; 77struct inpcbinfo ripcbinfo; 78 79/* 80 * Nominal space allocated to a raw ip socket. 81 */ 82#define RIPSNDQ 8192 83#define RIPRCVQ 8192 84 85/* 86 * Raw interface to IP protocol. 87 */ 88 89/* 90 * Initialize raw connection block q. 91 */ 92void 93rip_init() 94{ 95 LIST_INIT(&ripcb); 96 ripcbinfo.listhead = &ripcb; 97 /* 98 * XXX We don't use the hash list for raw IP, but it's easier 99 * to allocate a one entry hash list than it is to check all 100 * over the place for hashbase == NULL. 101 */ 102 ripcbinfo.hashbase = hashinit(1, M_PCB, &ripcbinfo.hashmask); 103 ripcbinfo.porthashbase = hashinit(1, M_PCB, &ripcbinfo.porthashmask); 104 ripcbinfo.ipi_zone = zinit("ripcb", sizeof(struct inpcb), 105 maxsockets, ZONE_INTERRUPT, 0); 106} 107 108static struct sockaddr_in ripsrc = { sizeof(ripsrc), AF_INET }; 109/* 110 * Setup generic address and protocol structures 111 * for raw_input routine, then pass them along with 112 * mbuf chain. 113 */ 114void
|
108rip_input(m, iphlen)
| 115rip_input(m, off, proto)
|
109 struct mbuf *m;
| 116 struct mbuf *m;
|
110 int iphlen;
| 117 int off, proto;
|
111{ 112 register struct ip *ip = mtod(m, struct ip *); 113 register struct inpcb *inp; 114 struct inpcb *last = 0; 115 struct mbuf *opts = 0; 116 117 ripsrc.sin_addr = ip->ip_src;
| 118{ 119 register struct ip *ip = mtod(m, struct ip *); 120 register struct inpcb *inp; 121 struct inpcb *last = 0; 122 struct mbuf *opts = 0; 123 124 ripsrc.sin_addr = ip->ip_src;
|
118 for (inp = ripcb.lh_first; inp != NULL; inp = inp->inp_list.le_next) { 119 if (inp->inp_ip_p && inp->inp_ip_p != ip->ip_p)
| 125 LIST_FOREACH(inp, &ripcb, inp_list) { 126#ifdef INET6 127 if ((inp->inp_vflag & INP_IPV4) == 0)
|
120 continue;
| 128 continue;
|
| 129#endif 130 if (inp->inp_ip_p && inp->inp_ip_p != proto) 131 continue;
|
121 if (inp->inp_laddr.s_addr && 122 inp->inp_laddr.s_addr != ip->ip_dst.s_addr) 123 continue; 124 if (inp->inp_faddr.s_addr && 125 inp->inp_faddr.s_addr != ip->ip_src.s_addr) 126 continue; 127 if (last) { 128 struct mbuf *n = m_copy(m, 0, (int)M_COPYALL); 129 if (n) { 130 if (last->inp_flags & INP_CONTROLOPTS || 131 last->inp_socket->so_options & SO_TIMESTAMP) 132 ip_savecontrol(last, &opts, ip, n); 133 if (sbappendaddr(&last->inp_socket->so_rcv, 134 (struct sockaddr *)&ripsrc, n, 135 opts) == 0) { 136 /* should notify about lost packet */ 137 m_freem(n); 138 if (opts) 139 m_freem(opts); 140 } else 141 sorwakeup(last->inp_socket); 142 opts = 0; 143 } 144 } 145 last = inp; 146 } 147 if (last) { 148 if (last->inp_flags & INP_CONTROLOPTS || 149 last->inp_socket->so_options & SO_TIMESTAMP) 150 ip_savecontrol(last, &opts, ip, m); 151 if (sbappendaddr(&last->inp_socket->so_rcv, 152 (struct sockaddr *)&ripsrc, m, opts) == 0) { 153 m_freem(m); 154 if (opts) 155 m_freem(opts); 156 } else 157 sorwakeup(last->inp_socket); 158 } else { 159 m_freem(m); 160 ipstat.ips_noproto++; 161 ipstat.ips_delivered--; 162 } 163} 164 165/* 166 * Generate IP header and pass packet to ip_output. 167 * Tack on options user may have setup with control call. 168 */ 169int 170rip_output(m, so, dst) 171 struct mbuf *m; 172 struct socket *so; 173 u_long dst; 174{ 175 register struct ip *ip; 176 register struct inpcb *inp = sotoinpcb(so); 177 int flags = (so->so_options & SO_DONTROUTE) | IP_ALLOWBROADCAST; 178 179 /* 180 * If the user handed us a complete IP packet, use it. 181 * Otherwise, allocate an mbuf for a header and fill it in. 182 */ 183 if ((inp->inp_flags & INP_HDRINCL) == 0) { 184 if (m->m_pkthdr.len + sizeof(struct ip) > IP_MAXPACKET) { 185 m_freem(m); 186 return(EMSGSIZE); 187 } 188 M_PREPEND(m, sizeof(struct ip), M_WAIT); 189 ip = mtod(m, struct ip *); 190 ip->ip_tos = 0; 191 ip->ip_off = 0; 192 ip->ip_p = inp->inp_ip_p; 193 ip->ip_len = m->m_pkthdr.len; 194 ip->ip_src = inp->inp_laddr; 195 ip->ip_dst.s_addr = dst; 196 ip->ip_ttl = MAXTTL; 197 } else { 198 if (m->m_pkthdr.len > IP_MAXPACKET) { 199 m_freem(m); 200 return(EMSGSIZE); 201 } 202 ip = mtod(m, struct ip *); 203 /* don't allow both user specified and setsockopt options, 204 and don't allow packet length sizes that will crash */ 205 if (((IP_VHL_HL(ip->ip_vhl) != (sizeof (*ip) >> 2)) 206 && inp->inp_options) 207 || (ip->ip_len > m->m_pkthdr.len) 208 || (ip->ip_len < (IP_VHL_HL(ip->ip_vhl) << 2))) { 209 m_freem(m); 210 return EINVAL; 211 } 212 if (ip->ip_id == 0) 213 ip->ip_id = htons(ip_id++); 214 /* XXX prevent ip_output from overwriting header fields */ 215 flags |= IP_RAWOUTPUT; 216 ipstat.ips_rawout++; 217 }
| 132 if (inp->inp_laddr.s_addr && 133 inp->inp_laddr.s_addr != ip->ip_dst.s_addr) 134 continue; 135 if (inp->inp_faddr.s_addr && 136 inp->inp_faddr.s_addr != ip->ip_src.s_addr) 137 continue; 138 if (last) { 139 struct mbuf *n = m_copy(m, 0, (int)M_COPYALL); 140 if (n) { 141 if (last->inp_flags & INP_CONTROLOPTS || 142 last->inp_socket->so_options & SO_TIMESTAMP) 143 ip_savecontrol(last, &opts, ip, n); 144 if (sbappendaddr(&last->inp_socket->so_rcv, 145 (struct sockaddr *)&ripsrc, n, 146 opts) == 0) { 147 /* should notify about lost packet */ 148 m_freem(n); 149 if (opts) 150 m_freem(opts); 151 } else 152 sorwakeup(last->inp_socket); 153 opts = 0; 154 } 155 } 156 last = inp; 157 } 158 if (last) { 159 if (last->inp_flags & INP_CONTROLOPTS || 160 last->inp_socket->so_options & SO_TIMESTAMP) 161 ip_savecontrol(last, &opts, ip, m); 162 if (sbappendaddr(&last->inp_socket->so_rcv, 163 (struct sockaddr *)&ripsrc, m, opts) == 0) { 164 m_freem(m); 165 if (opts) 166 m_freem(opts); 167 } else 168 sorwakeup(last->inp_socket); 169 } else { 170 m_freem(m); 171 ipstat.ips_noproto++; 172 ipstat.ips_delivered--; 173 } 174} 175 176/* 177 * Generate IP header and pass packet to ip_output. 178 * Tack on options user may have setup with control call. 179 */ 180int 181rip_output(m, so, dst) 182 struct mbuf *m; 183 struct socket *so; 184 u_long dst; 185{ 186 register struct ip *ip; 187 register struct inpcb *inp = sotoinpcb(so); 188 int flags = (so->so_options & SO_DONTROUTE) | IP_ALLOWBROADCAST; 189 190 /* 191 * If the user handed us a complete IP packet, use it. 192 * Otherwise, allocate an mbuf for a header and fill it in. 193 */ 194 if ((inp->inp_flags & INP_HDRINCL) == 0) { 195 if (m->m_pkthdr.len + sizeof(struct ip) > IP_MAXPACKET) { 196 m_freem(m); 197 return(EMSGSIZE); 198 } 199 M_PREPEND(m, sizeof(struct ip), M_WAIT); 200 ip = mtod(m, struct ip *); 201 ip->ip_tos = 0; 202 ip->ip_off = 0; 203 ip->ip_p = inp->inp_ip_p; 204 ip->ip_len = m->m_pkthdr.len; 205 ip->ip_src = inp->inp_laddr; 206 ip->ip_dst.s_addr = dst; 207 ip->ip_ttl = MAXTTL; 208 } else { 209 if (m->m_pkthdr.len > IP_MAXPACKET) { 210 m_freem(m); 211 return(EMSGSIZE); 212 } 213 ip = mtod(m, struct ip *); 214 /* don't allow both user specified and setsockopt options, 215 and don't allow packet length sizes that will crash */ 216 if (((IP_VHL_HL(ip->ip_vhl) != (sizeof (*ip) >> 2)) 217 && inp->inp_options) 218 || (ip->ip_len > m->m_pkthdr.len) 219 || (ip->ip_len < (IP_VHL_HL(ip->ip_vhl) << 2))) { 220 m_freem(m); 221 return EINVAL; 222 } 223 if (ip->ip_id == 0) 224 ip->ip_id = htons(ip_id++); 225 /* XXX prevent ip_output from overwriting header fields */ 226 flags |= IP_RAWOUTPUT; 227 ipstat.ips_rawout++; 228 }
|
218 return (ip_output(m, inp->inp_options, &inp->inp_route, flags,
| 229 230#ifdef IPSEC 231 m->m_pkthdr.rcvif = (struct ifnet *)so; /*XXX*/ 232#endif /*IPSEC*/ 233 234 return (ip_output(m, inp->inp_options, &inp->inp_route, 235 flags | IP_SOCKINMRCVIF,
|
219 inp->inp_moptions)); 220} 221 222/* 223 * Raw IP socket option processing. 224 */ 225int 226rip_ctloutput(so, sopt) 227 struct socket *so; 228 struct sockopt *sopt; 229{ 230 struct inpcb *inp = sotoinpcb(so); 231 int error, optval; 232 233 if (sopt->sopt_level != IPPROTO_IP) 234 return (EINVAL); 235 236 error = 0; 237 238 switch (sopt->sopt_dir) { 239 case SOPT_GET: 240 switch (sopt->sopt_name) { 241 case IP_HDRINCL: 242 optval = inp->inp_flags & INP_HDRINCL; 243 error = sooptcopyout(sopt, &optval, sizeof optval); 244 break; 245 246 case IP_FW_GET: 247 if (ip_fw_ctl_ptr == 0) 248 error = ENOPROTOOPT; 249 else 250 error = ip_fw_ctl_ptr(sopt); 251 break; 252 253#ifdef DUMMYNET 254 case IP_DUMMYNET_GET: 255 if (ip_dn_ctl_ptr == NULL) 256 error = ENOPROTOOPT ; 257 else 258 error = ip_dn_ctl_ptr(sopt); 259 break ; 260#endif /* DUMMYNET */ 261 262 case MRT_INIT: 263 case MRT_DONE: 264 case MRT_ADD_VIF: 265 case MRT_DEL_VIF: 266 case MRT_ADD_MFC: 267 case MRT_DEL_MFC: 268 case MRT_VERSION: 269 case MRT_ASSERT: 270 error = ip_mrouter_get(so, sopt); 271 break; 272 273 default: 274 error = ip_ctloutput(so, sopt); 275 break; 276 } 277 break; 278 279 case SOPT_SET: 280 switch (sopt->sopt_name) { 281 case IP_HDRINCL: 282 error = sooptcopyin(sopt, &optval, sizeof optval, 283 sizeof optval); 284 if (error) 285 break; 286 if (optval) 287 inp->inp_flags |= INP_HDRINCL; 288 else 289 inp->inp_flags &= ~INP_HDRINCL; 290 break; 291 292 case IP_FW_ADD: 293 case IP_FW_DEL: 294 case IP_FW_FLUSH: 295 case IP_FW_ZERO: 296 case IP_FW_RESETLOG: 297 if (ip_fw_ctl_ptr == 0) 298 error = ENOPROTOOPT; 299 else 300 error = ip_fw_ctl_ptr(sopt); 301 break; 302 303#ifdef DUMMYNET 304 case IP_DUMMYNET_CONFIGURE: 305 case IP_DUMMYNET_DEL: 306 case IP_DUMMYNET_FLUSH: 307 if (ip_dn_ctl_ptr == NULL) 308 error = ENOPROTOOPT ; 309 else 310 error = ip_dn_ctl_ptr(sopt); 311 break ; 312#endif 313 314 case IP_RSVP_ON: 315 error = ip_rsvp_init(so); 316 break; 317 318 case IP_RSVP_OFF: 319 error = ip_rsvp_done(); 320 break; 321 322 /* XXX - should be combined */ 323 case IP_RSVP_VIF_ON: 324 error = ip_rsvp_vif_init(so, sopt); 325 break; 326 327 case IP_RSVP_VIF_OFF: 328 error = ip_rsvp_vif_done(so, sopt); 329 break; 330 331 case MRT_INIT: 332 case MRT_DONE: 333 case MRT_ADD_VIF: 334 case MRT_DEL_VIF: 335 case MRT_ADD_MFC: 336 case MRT_DEL_MFC: 337 case MRT_VERSION: 338 case MRT_ASSERT: 339 error = ip_mrouter_set(so, sopt); 340 break; 341 342 default: 343 error = ip_ctloutput(so, sopt); 344 break; 345 } 346 break; 347 } 348 349 return (error); 350} 351 352/* 353 * This function exists solely to receive the PRC_IFDOWN messages which 354 * are sent by if_down(). It looks for an ifaddr whose ifa_addr is sa, 355 * and calls in_ifadown() to remove all routes corresponding to that address. 356 * It also receives the PRC_IFUP messages from if_up() and reinstalls the 357 * interface routes. 358 */ 359void 360rip_ctlinput(cmd, sa, vip) 361 int cmd; 362 struct sockaddr *sa; 363 void *vip; 364{ 365 struct in_ifaddr *ia; 366 struct ifnet *ifp; 367 int err; 368 int flags; 369 370 switch (cmd) { 371 case PRC_IFDOWN: 372 for (ia = in_ifaddrhead.tqh_first; ia; 373 ia = ia->ia_link.tqe_next) { 374 if (ia->ia_ifa.ifa_addr == sa 375 && (ia->ia_flags & IFA_ROUTE)) { 376 /* 377 * in_ifscrub kills the interface route. 378 */ 379 in_ifscrub(ia->ia_ifp, ia); 380 /* 381 * in_ifadown gets rid of all the rest of 382 * the routes. This is not quite the right 383 * thing to do, but at least if we are running 384 * a routing process they will come back. 385 */ 386 in_ifadown(&ia->ia_ifa); 387 break; 388 } 389 } 390 break; 391 392 case PRC_IFUP: 393 for (ia = in_ifaddrhead.tqh_first; ia; 394 ia = ia->ia_link.tqe_next) { 395 if (ia->ia_ifa.ifa_addr == sa) 396 break; 397 } 398 if (ia == 0 || (ia->ia_flags & IFA_ROUTE)) 399 return; 400 flags = RTF_UP; 401 ifp = ia->ia_ifa.ifa_ifp; 402 403 if ((ifp->if_flags & IFF_LOOPBACK) 404 || (ifp->if_flags & IFF_POINTOPOINT)) 405 flags |= RTF_HOST; 406 407 err = rtinit(&ia->ia_ifa, RTM_ADD, flags); 408 if (err == 0) 409 ia->ia_flags |= IFA_ROUTE; 410 break; 411 } 412} 413 414u_long rip_sendspace = RIPSNDQ; 415u_long rip_recvspace = RIPRCVQ; 416 417SYSCTL_INT(_net_inet_raw, OID_AUTO, maxdgram, CTLFLAG_RW, 418 &rip_sendspace, 0, "Maximum outgoing raw IP datagram size"); 419SYSCTL_INT(_net_inet_raw, OID_AUTO, recvspace, CTLFLAG_RW, 420 &rip_recvspace, 0, "Maximum incoming raw IP datagram size"); 421 422static int 423rip_attach(struct socket *so, int proto, struct proc *p) 424{ 425 struct inpcb *inp; 426 int error, s; 427 428 inp = sotoinpcb(so); 429 if (inp) 430 panic("rip_attach"); 431 if (p && (error = suser(p)) != 0) 432 return error; 433
| 236 inp->inp_moptions)); 237} 238 239/* 240 * Raw IP socket option processing. 241 */ 242int 243rip_ctloutput(so, sopt) 244 struct socket *so; 245 struct sockopt *sopt; 246{ 247 struct inpcb *inp = sotoinpcb(so); 248 int error, optval; 249 250 if (sopt->sopt_level != IPPROTO_IP) 251 return (EINVAL); 252 253 error = 0; 254 255 switch (sopt->sopt_dir) { 256 case SOPT_GET: 257 switch (sopt->sopt_name) { 258 case IP_HDRINCL: 259 optval = inp->inp_flags & INP_HDRINCL; 260 error = sooptcopyout(sopt, &optval, sizeof optval); 261 break; 262 263 case IP_FW_GET: 264 if (ip_fw_ctl_ptr == 0) 265 error = ENOPROTOOPT; 266 else 267 error = ip_fw_ctl_ptr(sopt); 268 break; 269 270#ifdef DUMMYNET 271 case IP_DUMMYNET_GET: 272 if (ip_dn_ctl_ptr == NULL) 273 error = ENOPROTOOPT ; 274 else 275 error = ip_dn_ctl_ptr(sopt); 276 break ; 277#endif /* DUMMYNET */ 278 279 case MRT_INIT: 280 case MRT_DONE: 281 case MRT_ADD_VIF: 282 case MRT_DEL_VIF: 283 case MRT_ADD_MFC: 284 case MRT_DEL_MFC: 285 case MRT_VERSION: 286 case MRT_ASSERT: 287 error = ip_mrouter_get(so, sopt); 288 break; 289 290 default: 291 error = ip_ctloutput(so, sopt); 292 break; 293 } 294 break; 295 296 case SOPT_SET: 297 switch (sopt->sopt_name) { 298 case IP_HDRINCL: 299 error = sooptcopyin(sopt, &optval, sizeof optval, 300 sizeof optval); 301 if (error) 302 break; 303 if (optval) 304 inp->inp_flags |= INP_HDRINCL; 305 else 306 inp->inp_flags &= ~INP_HDRINCL; 307 break; 308 309 case IP_FW_ADD: 310 case IP_FW_DEL: 311 case IP_FW_FLUSH: 312 case IP_FW_ZERO: 313 case IP_FW_RESETLOG: 314 if (ip_fw_ctl_ptr == 0) 315 error = ENOPROTOOPT; 316 else 317 error = ip_fw_ctl_ptr(sopt); 318 break; 319 320#ifdef DUMMYNET 321 case IP_DUMMYNET_CONFIGURE: 322 case IP_DUMMYNET_DEL: 323 case IP_DUMMYNET_FLUSH: 324 if (ip_dn_ctl_ptr == NULL) 325 error = ENOPROTOOPT ; 326 else 327 error = ip_dn_ctl_ptr(sopt); 328 break ; 329#endif 330 331 case IP_RSVP_ON: 332 error = ip_rsvp_init(so); 333 break; 334 335 case IP_RSVP_OFF: 336 error = ip_rsvp_done(); 337 break; 338 339 /* XXX - should be combined */ 340 case IP_RSVP_VIF_ON: 341 error = ip_rsvp_vif_init(so, sopt); 342 break; 343 344 case IP_RSVP_VIF_OFF: 345 error = ip_rsvp_vif_done(so, sopt); 346 break; 347 348 case MRT_INIT: 349 case MRT_DONE: 350 case MRT_ADD_VIF: 351 case MRT_DEL_VIF: 352 case MRT_ADD_MFC: 353 case MRT_DEL_MFC: 354 case MRT_VERSION: 355 case MRT_ASSERT: 356 error = ip_mrouter_set(so, sopt); 357 break; 358 359 default: 360 error = ip_ctloutput(so, sopt); 361 break; 362 } 363 break; 364 } 365 366 return (error); 367} 368 369/* 370 * This function exists solely to receive the PRC_IFDOWN messages which 371 * are sent by if_down(). It looks for an ifaddr whose ifa_addr is sa, 372 * and calls in_ifadown() to remove all routes corresponding to that address. 373 * It also receives the PRC_IFUP messages from if_up() and reinstalls the 374 * interface routes. 375 */ 376void 377rip_ctlinput(cmd, sa, vip) 378 int cmd; 379 struct sockaddr *sa; 380 void *vip; 381{ 382 struct in_ifaddr *ia; 383 struct ifnet *ifp; 384 int err; 385 int flags; 386 387 switch (cmd) { 388 case PRC_IFDOWN: 389 for (ia = in_ifaddrhead.tqh_first; ia; 390 ia = ia->ia_link.tqe_next) { 391 if (ia->ia_ifa.ifa_addr == sa 392 && (ia->ia_flags & IFA_ROUTE)) { 393 /* 394 * in_ifscrub kills the interface route. 395 */ 396 in_ifscrub(ia->ia_ifp, ia); 397 /* 398 * in_ifadown gets rid of all the rest of 399 * the routes. This is not quite the right 400 * thing to do, but at least if we are running 401 * a routing process they will come back. 402 */ 403 in_ifadown(&ia->ia_ifa); 404 break; 405 } 406 } 407 break; 408 409 case PRC_IFUP: 410 for (ia = in_ifaddrhead.tqh_first; ia; 411 ia = ia->ia_link.tqe_next) { 412 if (ia->ia_ifa.ifa_addr == sa) 413 break; 414 } 415 if (ia == 0 || (ia->ia_flags & IFA_ROUTE)) 416 return; 417 flags = RTF_UP; 418 ifp = ia->ia_ifa.ifa_ifp; 419 420 if ((ifp->if_flags & IFF_LOOPBACK) 421 || (ifp->if_flags & IFF_POINTOPOINT)) 422 flags |= RTF_HOST; 423 424 err = rtinit(&ia->ia_ifa, RTM_ADD, flags); 425 if (err == 0) 426 ia->ia_flags |= IFA_ROUTE; 427 break; 428 } 429} 430 431u_long rip_sendspace = RIPSNDQ; 432u_long rip_recvspace = RIPRCVQ; 433 434SYSCTL_INT(_net_inet_raw, OID_AUTO, maxdgram, CTLFLAG_RW, 435 &rip_sendspace, 0, "Maximum outgoing raw IP datagram size"); 436SYSCTL_INT(_net_inet_raw, OID_AUTO, recvspace, CTLFLAG_RW, 437 &rip_recvspace, 0, "Maximum incoming raw IP datagram size"); 438 439static int 440rip_attach(struct socket *so, int proto, struct proc *p) 441{ 442 struct inpcb *inp; 443 int error, s; 444 445 inp = sotoinpcb(so); 446 if (inp) 447 panic("rip_attach"); 448 if (p && (error = suser(p)) != 0) 449 return error; 450
|
| 451 error = soreserve(so, rip_sendspace, rip_recvspace); 452 if (error) 453 return error;
|
434 s = splnet(); 435 error = in_pcballoc(so, &ripcbinfo, p); 436 splx(s); 437 if (error) 438 return error;
| 454 s = splnet(); 455 error = in_pcballoc(so, &ripcbinfo, p); 456 splx(s); 457 if (error) 458 return error;
|
439 error = soreserve(so, rip_sendspace, rip_recvspace); 440 if (error) 441 return error;
| |
442 inp = (struct inpcb *)so->so_pcb;
| 459 inp = (struct inpcb *)so->so_pcb;
|
| 460 inp->inp_vflag |= INP_IPV4;
|
443 inp->inp_ip_p = proto;
| 461 inp->inp_ip_p = proto;
|
| 462#ifdef IPSEC 463 error = ipsec_init_policy(so, &inp->inp_sp); 464 if (error != 0) { 465 in_pcbdetach(inp); 466 return error; 467 } 468#endif /*IPSEC*/
|
444 return 0; 445} 446 447static int 448rip_detach(struct socket *so) 449{ 450 struct inpcb *inp; 451 452 inp = sotoinpcb(so); 453 if (inp == 0) 454 panic("rip_detach"); 455 if (so == ip_mrouter) 456 ip_mrouter_done(); 457 ip_rsvp_force_done(so); 458 if (so == ip_rsvpd) 459 ip_rsvp_done(); 460 in_pcbdetach(inp); 461 return 0; 462} 463 464static int 465rip_abort(struct socket *so) 466{ 467 soisdisconnected(so); 468 return rip_detach(so); 469} 470 471static int 472rip_disconnect(struct socket *so) 473{ 474 if ((so->so_state & SS_ISCONNECTED) == 0) 475 return ENOTCONN; 476 return rip_abort(so); 477} 478 479static int 480rip_bind(struct socket *so, struct sockaddr *nam, struct proc *p) 481{ 482 struct inpcb *inp = sotoinpcb(so); 483 struct sockaddr_in *addr = (struct sockaddr_in *)nam; 484 485 if (nam->sa_len != sizeof(*addr)) 486 return EINVAL; 487 488 if (TAILQ_EMPTY(&ifnet) || ((addr->sin_family != AF_INET) && 489 (addr->sin_family != AF_IMPLINK)) || 490 (addr->sin_addr.s_addr && 491 ifa_ifwithaddr((struct sockaddr *)addr) == 0)) 492 return EADDRNOTAVAIL; 493 inp->inp_laddr = addr->sin_addr; 494 return 0; 495} 496 497static int 498rip_connect(struct socket *so, struct sockaddr *nam, struct proc *p) 499{ 500 struct inpcb *inp = sotoinpcb(so); 501 struct sockaddr_in *addr = (struct sockaddr_in *)nam; 502 503 if (nam->sa_len != sizeof(*addr)) 504 return EINVAL; 505 if (TAILQ_EMPTY(&ifnet)) 506 return EADDRNOTAVAIL; 507 if ((addr->sin_family != AF_INET) && 508 (addr->sin_family != AF_IMPLINK)) 509 return EAFNOSUPPORT; 510 inp->inp_faddr = addr->sin_addr; 511 soisconnected(so); 512 return 0; 513} 514 515static int 516rip_shutdown(struct socket *so) 517{ 518 socantsendmore(so); 519 return 0; 520} 521 522static int 523rip_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, 524 struct mbuf *control, struct proc *p) 525{ 526 struct inpcb *inp = sotoinpcb(so); 527 register u_long dst; 528 529 if (so->so_state & SS_ISCONNECTED) { 530 if (nam) { 531 m_freem(m); 532 return EISCONN; 533 } 534 dst = inp->inp_faddr.s_addr; 535 } else { 536 if (nam == NULL) { 537 m_freem(m); 538 return ENOTCONN; 539 } 540 dst = ((struct sockaddr_in *)nam)->sin_addr.s_addr; 541 } 542 return rip_output(m, so, dst); 543} 544 545static int 546rip_pcblist SYSCTL_HANDLER_ARGS 547{ 548 int error, i, n, s; 549 struct inpcb *inp, **inp_list; 550 inp_gen_t gencnt; 551 struct xinpgen xig; 552 553 /* 554 * The process of preparing the TCB list is too time-consuming and 555 * resource-intensive to repeat twice on every request. 556 */ 557 if (req->oldptr == 0) { 558 n = ripcbinfo.ipi_count; 559 req->oldidx = 2 * (sizeof xig) 560 + (n + n/8) * sizeof(struct xinpcb); 561 return 0; 562 } 563 564 if (req->newptr != 0) 565 return EPERM; 566 567 /* 568 * OK, now we're committed to doing something. 569 */ 570 s = splnet(); 571 gencnt = ripcbinfo.ipi_gencnt; 572 n = ripcbinfo.ipi_count; 573 splx(s); 574 575 xig.xig_len = sizeof xig; 576 xig.xig_count = n; 577 xig.xig_gen = gencnt; 578 xig.xig_sogen = so_gencnt; 579 error = SYSCTL_OUT(req, &xig, sizeof xig); 580 if (error) 581 return error; 582 583 inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK); 584 if (inp_list == 0) 585 return ENOMEM; 586 587 s = splnet(); 588 for (inp = ripcbinfo.listhead->lh_first, i = 0; inp && i < n; 589 inp = inp->inp_list.le_next) { 590 if (inp->inp_gencnt <= gencnt) 591 inp_list[i++] = inp; 592 } 593 splx(s); 594 n = i; 595 596 error = 0; 597 for (i = 0; i < n; i++) { 598 inp = inp_list[i]; 599 if (inp->inp_gencnt <= gencnt) { 600 struct xinpcb xi; 601 xi.xi_len = sizeof xi; 602 /* XXX should avoid extra copy */ 603 bcopy(inp, &xi.xi_inp, sizeof *inp); 604 if (inp->inp_socket) 605 sotoxsocket(inp->inp_socket, &xi.xi_socket); 606 error = SYSCTL_OUT(req, &xi, sizeof xi); 607 } 608 } 609 if (!error) { 610 /* 611 * Give the user an updated idea of our state. 612 * If the generation differs from what we told 613 * her before, she knows that something happened 614 * while we were processing this request, and it 615 * might be necessary to retry. 616 */ 617 s = splnet(); 618 xig.xig_gen = ripcbinfo.ipi_gencnt; 619 xig.xig_sogen = so_gencnt; 620 xig.xig_count = ripcbinfo.ipi_count; 621 splx(s); 622 error = SYSCTL_OUT(req, &xig, sizeof xig); 623 } 624 free(inp_list, M_TEMP); 625 return error; 626} 627 628SYSCTL_PROC(_net_inet_raw, OID_AUTO/*XXX*/, pcblist, CTLFLAG_RD, 0, 0, 629 rip_pcblist, "S,xinpcb", "List of active raw IP sockets"); 630 631struct pr_usrreqs rip_usrreqs = { 632 rip_abort, pru_accept_notsupp, rip_attach, rip_bind, rip_connect, 633 pru_connect2_notsupp, in_control, rip_detach, rip_disconnect, 634 pru_listen_notsupp, in_setpeeraddr, pru_rcvd_notsupp, 635 pru_rcvoob_notsupp, rip_send, pru_sense_null, rip_shutdown, 636 in_setsockaddr, sosend, soreceive, sopoll 637};
| 469 return 0; 470} 471 472static int 473rip_detach(struct socket *so) 474{ 475 struct inpcb *inp; 476 477 inp = sotoinpcb(so); 478 if (inp == 0) 479 panic("rip_detach"); 480 if (so == ip_mrouter) 481 ip_mrouter_done(); 482 ip_rsvp_force_done(so); 483 if (so == ip_rsvpd) 484 ip_rsvp_done(); 485 in_pcbdetach(inp); 486 return 0; 487} 488 489static int 490rip_abort(struct socket *so) 491{ 492 soisdisconnected(so); 493 return rip_detach(so); 494} 495 496static int 497rip_disconnect(struct socket *so) 498{ 499 if ((so->so_state & SS_ISCONNECTED) == 0) 500 return ENOTCONN; 501 return rip_abort(so); 502} 503 504static int 505rip_bind(struct socket *so, struct sockaddr *nam, struct proc *p) 506{ 507 struct inpcb *inp = sotoinpcb(so); 508 struct sockaddr_in *addr = (struct sockaddr_in *)nam; 509 510 if (nam->sa_len != sizeof(*addr)) 511 return EINVAL; 512 513 if (TAILQ_EMPTY(&ifnet) || ((addr->sin_family != AF_INET) && 514 (addr->sin_family != AF_IMPLINK)) || 515 (addr->sin_addr.s_addr && 516 ifa_ifwithaddr((struct sockaddr *)addr) == 0)) 517 return EADDRNOTAVAIL; 518 inp->inp_laddr = addr->sin_addr; 519 return 0; 520} 521 522static int 523rip_connect(struct socket *so, struct sockaddr *nam, struct proc *p) 524{ 525 struct inpcb *inp = sotoinpcb(so); 526 struct sockaddr_in *addr = (struct sockaddr_in *)nam; 527 528 if (nam->sa_len != sizeof(*addr)) 529 return EINVAL; 530 if (TAILQ_EMPTY(&ifnet)) 531 return EADDRNOTAVAIL; 532 if ((addr->sin_family != AF_INET) && 533 (addr->sin_family != AF_IMPLINK)) 534 return EAFNOSUPPORT; 535 inp->inp_faddr = addr->sin_addr; 536 soisconnected(so); 537 return 0; 538} 539 540static int 541rip_shutdown(struct socket *so) 542{ 543 socantsendmore(so); 544 return 0; 545} 546 547static int 548rip_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, 549 struct mbuf *control, struct proc *p) 550{ 551 struct inpcb *inp = sotoinpcb(so); 552 register u_long dst; 553 554 if (so->so_state & SS_ISCONNECTED) { 555 if (nam) { 556 m_freem(m); 557 return EISCONN; 558 } 559 dst = inp->inp_faddr.s_addr; 560 } else { 561 if (nam == NULL) { 562 m_freem(m); 563 return ENOTCONN; 564 } 565 dst = ((struct sockaddr_in *)nam)->sin_addr.s_addr; 566 } 567 return rip_output(m, so, dst); 568} 569 570static int 571rip_pcblist SYSCTL_HANDLER_ARGS 572{ 573 int error, i, n, s; 574 struct inpcb *inp, **inp_list; 575 inp_gen_t gencnt; 576 struct xinpgen xig; 577 578 /* 579 * The process of preparing the TCB list is too time-consuming and 580 * resource-intensive to repeat twice on every request. 581 */ 582 if (req->oldptr == 0) { 583 n = ripcbinfo.ipi_count; 584 req->oldidx = 2 * (sizeof xig) 585 + (n + n/8) * sizeof(struct xinpcb); 586 return 0; 587 } 588 589 if (req->newptr != 0) 590 return EPERM; 591 592 /* 593 * OK, now we're committed to doing something. 594 */ 595 s = splnet(); 596 gencnt = ripcbinfo.ipi_gencnt; 597 n = ripcbinfo.ipi_count; 598 splx(s); 599 600 xig.xig_len = sizeof xig; 601 xig.xig_count = n; 602 xig.xig_gen = gencnt; 603 xig.xig_sogen = so_gencnt; 604 error = SYSCTL_OUT(req, &xig, sizeof xig); 605 if (error) 606 return error; 607 608 inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK); 609 if (inp_list == 0) 610 return ENOMEM; 611 612 s = splnet(); 613 for (inp = ripcbinfo.listhead->lh_first, i = 0; inp && i < n; 614 inp = inp->inp_list.le_next) { 615 if (inp->inp_gencnt <= gencnt) 616 inp_list[i++] = inp; 617 } 618 splx(s); 619 n = i; 620 621 error = 0; 622 for (i = 0; i < n; i++) { 623 inp = inp_list[i]; 624 if (inp->inp_gencnt <= gencnt) { 625 struct xinpcb xi; 626 xi.xi_len = sizeof xi; 627 /* XXX should avoid extra copy */ 628 bcopy(inp, &xi.xi_inp, sizeof *inp); 629 if (inp->inp_socket) 630 sotoxsocket(inp->inp_socket, &xi.xi_socket); 631 error = SYSCTL_OUT(req, &xi, sizeof xi); 632 } 633 } 634 if (!error) { 635 /* 636 * Give the user an updated idea of our state. 637 * If the generation differs from what we told 638 * her before, she knows that something happened 639 * while we were processing this request, and it 640 * might be necessary to retry. 641 */ 642 s = splnet(); 643 xig.xig_gen = ripcbinfo.ipi_gencnt; 644 xig.xig_sogen = so_gencnt; 645 xig.xig_count = ripcbinfo.ipi_count; 646 splx(s); 647 error = SYSCTL_OUT(req, &xig, sizeof xig); 648 } 649 free(inp_list, M_TEMP); 650 return error; 651} 652 653SYSCTL_PROC(_net_inet_raw, OID_AUTO/*XXX*/, pcblist, CTLFLAG_RD, 0, 0, 654 rip_pcblist, "S,xinpcb", "List of active raw IP sockets"); 655 656struct pr_usrreqs rip_usrreqs = { 657 rip_abort, pru_accept_notsupp, rip_attach, rip_bind, rip_connect, 658 pru_connect2_notsupp, in_control, rip_detach, rip_disconnect, 659 pru_listen_notsupp, in_setpeeraddr, pru_rcvd_notsupp, 660 pru_rcvoob_notsupp, rip_send, pru_sense_null, rip_shutdown, 661 in_setsockaddr, sosend, soreceive, sopoll 662};
|