raw_ip.c revision 1.53.4.1 1 /* $NetBSD: raw_ip.c,v 1.53.4.1 2001/02/26 22:43:55 he Exp $ */
2
3 /*
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 /*
33 * Copyright (c) 1982, 1986, 1988, 1993
34 * The Regents of the University of California. All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. All advertising materials mentioning features or use of this software
45 * must display the following acknowledgement:
46 * This product includes software developed by the University of
47 * California, Berkeley and its contributors.
48 * 4. Neither the name of the University nor the names of its contributors
49 * may be used to endorse or promote products derived from this software
50 * without specific prior written permission.
51 *
52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * SUCH DAMAGE.
63 *
64 * @(#)raw_ip.c 8.7 (Berkeley) 5/15/95
65 */
66
67 #include "opt_ipsec.h"
68 #include "opt_mrouting.h"
69
70 #include <sys/param.h>
71 #include <sys/malloc.h>
72 #include <sys/mbuf.h>
73 #include <sys/socket.h>
74 #include <sys/protosw.h>
75 #include <sys/socketvar.h>
76 #include <sys/errno.h>
77 #include <sys/systm.h>
78 #include <sys/proc.h>
79
80 #include <net/if.h>
81 #include <net/route.h>
82
83 #include <netinet/in.h>
84 #include <netinet/in_systm.h>
85 #include <netinet/ip.h>
86 #include <netinet/ip_var.h>
87 #include <netinet/ip_mroute.h>
88 #include <netinet/ip_icmp.h>
89 #include <netinet/in_pcb.h>
90 #include <netinet/in_var.h>
91
92 #include <machine/stdarg.h>
93
94 #ifdef IPSEC
95 #include <netinet6/ipsec.h>
96 #endif /*IPSEC*/
97
98 struct inpcbtable rawcbtable;
99
100 int rip_bind __P((struct inpcb *, struct mbuf *));
101 int rip_connect __P((struct inpcb *, struct mbuf *));
102 void rip_disconnect __P((struct inpcb *));
103
104 /*
105 * Nominal space allocated to a raw ip socket.
106 */
107 #define RIPSNDQ 8192
108 #define RIPRCVQ 8192
109
110 /*
111 * Raw interface to IP protocol.
112 */
113
114 /*
115 * Initialize raw connection block q.
116 */
117 void
118 rip_init()
119 {
120
121 in_pcbinit(&rawcbtable, 1, 1);
122 }
123
124 static struct sockaddr_in ripsrc = { sizeof(ripsrc), AF_INET };
125
126 /*
127 * Setup generic address and protocol structures
128 * for raw_input routine, then pass them along with
129 * mbuf chain.
130 */
131 void
132 #if __STDC__
133 rip_input(struct mbuf *m, ...)
134 #else
135 rip_input(m, va_alist)
136 struct mbuf *m;
137 va_dcl
138 #endif
139 {
140 int off, proto;
141 struct ip *ip = mtod(m, struct ip *);
142 struct inpcb *inp;
143 struct inpcb *last = 0;
144 struct mbuf *opts = 0;
145 struct sockaddr_in ripsrc;
146 va_list ap;
147
148 va_start(ap, m);
149 off = va_arg(ap, int);
150 proto = va_arg(ap, int);
151 va_end(ap);
152
153 ripsrc.sin_family = AF_INET;
154 ripsrc.sin_len = sizeof(struct sockaddr_in);
155 ripsrc.sin_addr = ip->ip_src;
156 ripsrc.sin_port = 0;
157 bzero((caddr_t)ripsrc.sin_zero, sizeof(ripsrc.sin_zero));
158
159 /*
160 * XXX Compatibility: programs using raw IP expect ip_len
161 * XXX to have the header length subtracted.
162 */
163 ip->ip_len -= ip->ip_hl << 2;
164
165 for (inp = rawcbtable.inpt_queue.cqh_first;
166 inp != (struct inpcb *)&rawcbtable.inpt_queue;
167 inp = inp->inp_queue.cqe_next) {
168 if (inp->inp_ip.ip_p && inp->inp_ip.ip_p != proto)
169 continue;
170 if (!in_nullhost(inp->inp_laddr) &&
171 !in_hosteq(inp->inp_laddr, ip->ip_dst))
172 continue;
173 if (!in_nullhost(inp->inp_faddr) &&
174 !in_hosteq(inp->inp_faddr, ip->ip_src))
175 continue;
176 if (last) {
177 struct mbuf *n;
178
179 #ifdef IPSEC
180 /* check AH/ESP integrity. */
181 if (ipsec4_in_reject_so(m, last->inp_socket)) {
182 ipsecstat.in_polvio++;
183 /* do not inject data to pcb */
184 } else
185 #endif /*IPSEC*/
186 if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
187 if (last->inp_flags & INP_CONTROLOPTS ||
188 last->inp_socket->so_options & SO_TIMESTAMP)
189 ip_savecontrol(last, &opts, ip, n);
190 if (sbappendaddr(&last->inp_socket->so_rcv,
191 sintosa(&ripsrc), n, opts) == 0) {
192 /* should notify about lost packet */
193 m_freem(n);
194 if (opts)
195 m_freem(opts);
196 } else
197 sorwakeup(last->inp_socket);
198 opts = NULL;
199 }
200 }
201 last = inp;
202 }
203 #ifdef IPSEC
204 /* check AH/ESP integrity. */
205 if (last && ipsec4_in_reject_so(m, last->inp_socket)) {
206 m_freem(m);
207 ipsecstat.in_polvio++;
208 ipstat.ips_delivered--;
209 /* do not inject data to pcb */
210 } else
211 #endif /*IPSEC*/
212 if (last) {
213 if (last->inp_flags & INP_CONTROLOPTS ||
214 last->inp_socket->so_options & SO_TIMESTAMP)
215 ip_savecontrol(last, &opts, ip, m);
216 if (sbappendaddr(&last->inp_socket->so_rcv,
217 sintosa(&ripsrc), m, opts) == 0) {
218 m_freem(m);
219 if (opts)
220 m_freem(opts);
221 } else
222 sorwakeup(last->inp_socket);
223 } else {
224 if (inetsw[ip_protox[ip->ip_p]].pr_input == rip_input) {
225 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PROTOCOL,
226 0, 0);
227 ipstat.ips_noproto++;
228 ipstat.ips_delivered--;
229 } else
230 m_freem(m);
231 }
232 return;
233 }
234
235 /*
236 * Generate IP header and pass packet to ip_output.
237 * Tack on options user may have setup with control call.
238 */
239 int
240 #if __STDC__
241 rip_output(struct mbuf *m, ...)
242 #else
243 rip_output(m, va_alist)
244 struct mbuf *m;
245 va_dcl
246 #endif
247 {
248 struct inpcb *inp;
249 struct ip *ip;
250 struct mbuf *opts;
251 int flags;
252 va_list ap;
253
254 va_start(ap, m);
255 inp = va_arg(ap, struct inpcb *);
256 va_end(ap);
257
258 flags =
259 (inp->inp_socket->so_options & SO_DONTROUTE) | IP_ALLOWBROADCAST
260 | IP_RETURNMTU;
261
262 /*
263 * If the user handed us a complete IP packet, use it.
264 * Otherwise, allocate an mbuf for a header and fill it in.
265 */
266 if ((inp->inp_flags & INP_HDRINCL) == 0) {
267 if ((m->m_pkthdr.len + sizeof(struct ip)) > IP_MAXPACKET) {
268 m_freem(m);
269 return (EMSGSIZE);
270 }
271 M_PREPEND(m, sizeof(struct ip), M_WAIT);
272 ip = mtod(m, struct ip *);
273 ip->ip_tos = 0;
274 ip->ip_off = 0;
275 ip->ip_p = inp->inp_ip.ip_p;
276 ip->ip_len = m->m_pkthdr.len;
277 ip->ip_src = inp->inp_laddr;
278 ip->ip_dst = inp->inp_faddr;
279 ip->ip_ttl = MAXTTL;
280 opts = inp->inp_options;
281 } else {
282 if (m->m_pkthdr.len > IP_MAXPACKET) {
283 m_freem(m);
284 return (EMSGSIZE);
285 }
286 ip = mtod(m, struct ip *);
287 if (m->m_pkthdr.len != ip->ip_len) {
288 m_freem(m);
289 return (EINVAL);
290 }
291 if (ip->ip_id == 0)
292 ip->ip_id = htons(ip_id++);
293 opts = NULL;
294 /* XXX prevent ip_output from overwriting header fields */
295 flags |= IP_RAWOUTPUT;
296 ipstat.ips_rawout++;
297 }
298 #ifdef IPSEC
299 ipsec_setsocket(m, inp->inp_socket);
300 #endif /*IPSEC*/
301 return (ip_output(m, opts, &inp->inp_route, flags, inp->inp_moptions, &inp->inp_errormtu));
302 }
303
304 /*
305 * Raw IP socket option processing.
306 */
307 int
308 rip_ctloutput(op, so, level, optname, m)
309 int op;
310 struct socket *so;
311 int level, optname;
312 struct mbuf **m;
313 {
314 struct inpcb *inp = sotoinpcb(so);
315 int error = 0;
316
317 if (level != IPPROTO_IP) {
318 error = ENOPROTOOPT;
319 if (op == PRCO_SETOPT && *m != 0)
320 (void) m_free(*m);
321 } else switch (op) {
322
323 case PRCO_SETOPT:
324 switch (optname) {
325 case IP_HDRINCL:
326 if (*m == 0 || (*m)->m_len < sizeof (int))
327 error = EINVAL;
328 else {
329 if (*mtod(*m, int *))
330 inp->inp_flags |= INP_HDRINCL;
331 else
332 inp->inp_flags &= ~INP_HDRINCL;
333 }
334 if (*m != 0)
335 (void) m_free(*m);
336 break;
337
338 #ifdef MROUTING
339 case MRT_INIT:
340 case MRT_DONE:
341 case MRT_ADD_VIF:
342 case MRT_DEL_VIF:
343 case MRT_ADD_MFC:
344 case MRT_DEL_MFC:
345 case MRT_ASSERT:
346 error = ip_mrouter_set(so, optname, m);
347 break;
348 #endif
349
350 default:
351 error = ip_ctloutput(op, so, level, optname, m);
352 break;
353 }
354 break;
355
356 case PRCO_GETOPT:
357 switch (optname) {
358 case IP_HDRINCL:
359 *m = m_get(M_WAIT, M_SOOPTS);
360 (*m)->m_len = sizeof (int);
361 *mtod(*m, int *) = inp->inp_flags & INP_HDRINCL ? 1 : 0;
362 break;
363
364 #ifdef MROUTING
365 case MRT_VERSION:
366 case MRT_ASSERT:
367 error = ip_mrouter_get(so, optname, m);
368 break;
369 #endif
370
371 default:
372 error = ip_ctloutput(op, so, level, optname, m);
373 break;
374 }
375 break;
376 }
377 return (error);
378 }
379
380 int
381 rip_bind(inp, nam)
382 struct inpcb *inp;
383 struct mbuf *nam;
384 {
385 struct sockaddr_in *addr = mtod(nam, struct sockaddr_in *);
386
387 if (nam->m_len != sizeof(*addr))
388 return (EINVAL);
389 if (ifnet.tqh_first == 0)
390 return (EADDRNOTAVAIL);
391 if (addr->sin_family != AF_INET &&
392 addr->sin_family != AF_IMPLINK)
393 return (EAFNOSUPPORT);
394 if (!in_nullhost(addr->sin_addr) &&
395 ifa_ifwithaddr(sintosa(addr)) == 0)
396 return (EADDRNOTAVAIL);
397 inp->inp_laddr = addr->sin_addr;
398 return (0);
399 }
400
401 int
402 rip_connect(inp, nam)
403 struct inpcb *inp;
404 struct mbuf *nam;
405 {
406 struct sockaddr_in *addr = mtod(nam, struct sockaddr_in *);
407
408 if (nam->m_len != sizeof(*addr))
409 return (EINVAL);
410 if (ifnet.tqh_first == 0)
411 return (EADDRNOTAVAIL);
412 if (addr->sin_family != AF_INET &&
413 addr->sin_family != AF_IMPLINK)
414 return (EAFNOSUPPORT);
415 inp->inp_faddr = addr->sin_addr;
416 return (0);
417 }
418
419 void
420 rip_disconnect(inp)
421 struct inpcb *inp;
422 {
423
424 inp->inp_faddr = zeroin_addr;
425 }
426
427 u_long rip_sendspace = RIPSNDQ;
428 u_long rip_recvspace = RIPRCVQ;
429
430 /*ARGSUSED*/
431 int
432 rip_usrreq(so, req, m, nam, control, p)
433 struct socket *so;
434 int req;
435 struct mbuf *m, *nam, *control;
436 struct proc *p;
437 {
438 struct inpcb *inp;
439 int s;
440 int error = 0;
441 #ifdef MROUTING
442 extern struct socket *ip_mrouter;
443 #endif
444
445 if (req == PRU_CONTROL)
446 return (in_control(so, (long)m, (caddr_t)nam,
447 (struct ifnet *)control, p));
448
449 if (req == PRU_PURGEIF) {
450 in_purgeif((struct ifnet *)control);
451 in_pcbpurgeif(&rawcbtable, (struct ifnet *)control);
452 return (0);
453 }
454
455 s = splsoftnet();
456 inp = sotoinpcb(so);
457 #ifdef DIAGNOSTIC
458 if (req != PRU_SEND && req != PRU_SENDOOB && control)
459 panic("rip_usrreq: unexpected control mbuf");
460 #endif
461 if (inp == 0 && req != PRU_ATTACH) {
462 error = EINVAL;
463 goto release;
464 }
465
466 switch (req) {
467
468 case PRU_ATTACH:
469 if (inp != 0) {
470 error = EISCONN;
471 break;
472 }
473 if (p == 0 || (error = suser(p->p_ucred, &p->p_acflag))) {
474 error = EACCES;
475 break;
476 }
477 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
478 error = soreserve(so, rip_sendspace, rip_recvspace);
479 if (error)
480 break;
481 }
482 error = in_pcballoc(so, &rawcbtable);
483 if (error)
484 break;
485 inp = sotoinpcb(so);
486 inp->inp_ip.ip_p = (long)nam;
487 #ifdef IPSEC
488 error = ipsec_init_policy(so, &inp->inp_sp);
489 if (error != 0) {
490 in_pcbdetach(inp);
491 break;
492 }
493 #endif /*IPSEC*/
494 break;
495
496 case PRU_DETACH:
497 #ifdef MROUTING
498 if (so == ip_mrouter)
499 ip_mrouter_done();
500 #endif
501 in_pcbdetach(inp);
502 break;
503
504 case PRU_BIND:
505 error = rip_bind(inp, nam);
506 break;
507
508 case PRU_LISTEN:
509 error = EOPNOTSUPP;
510 break;
511
512 case PRU_CONNECT:
513 error = rip_connect(inp, nam);
514 if (error)
515 break;
516 soisconnected(so);
517 break;
518
519 case PRU_CONNECT2:
520 error = EOPNOTSUPP;
521 break;
522
523 case PRU_DISCONNECT:
524 soisdisconnected(so);
525 rip_disconnect(inp);
526 break;
527
528 /*
529 * Mark the connection as being incapable of further input.
530 */
531 case PRU_SHUTDOWN:
532 socantsendmore(so);
533 break;
534
535 case PRU_RCVD:
536 error = EOPNOTSUPP;
537 break;
538
539 /*
540 * Ship a packet out. The appropriate raw output
541 * routine handles any massaging necessary.
542 */
543 case PRU_SEND:
544 if (control && control->m_len) {
545 m_freem(control);
546 m_freem(m);
547 error = EINVAL;
548 break;
549 }
550 {
551 if (nam) {
552 if ((so->so_state & SS_ISCONNECTED) != 0) {
553 error = EISCONN;
554 goto die;
555 }
556 error = rip_connect(inp, nam);
557 if (error) {
558 die:
559 m_freem(m);
560 break;
561 }
562 } else {
563 if ((so->so_state & SS_ISCONNECTED) == 0) {
564 error = ENOTCONN;
565 goto die;
566 }
567 }
568 error = rip_output(m, inp);
569 if (nam)
570 rip_disconnect(inp);
571 }
572 break;
573
574 case PRU_SENSE:
575 /*
576 * stat: don't bother with a blocksize.
577 */
578 splx(s);
579 return (0);
580
581 case PRU_RCVOOB:
582 error = EOPNOTSUPP;
583 break;
584
585 case PRU_SENDOOB:
586 m_freem(control);
587 m_freem(m);
588 error = EOPNOTSUPP;
589 break;
590
591 case PRU_SOCKADDR:
592 in_setsockaddr(inp, nam);
593 break;
594
595 case PRU_PEERADDR:
596 in_setpeeraddr(inp, nam);
597 break;
598
599 default:
600 panic("rip_usrreq");
601 }
602
603 release:
604 splx(s);
605 return (error);
606 }
607