]>
Commit | Line | Data |
---|---|---|
1c79356b A |
1 | /* |
2 | * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. | |
3 | * 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. Neither the name of the project nor the names of its contributors | |
14 | * may be used to endorse or promote products derived from this software | |
15 | * without specific prior written permission. | |
16 | * | |
17 | * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND | |
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE | |
21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
27 | * SUCH DAMAGE. | |
28 | */ | |
29 | ||
30 | /* | |
31 | * Copyright (c) 1982, 1986, 1988, 1993 | |
32 | * The Regents of the University of California. All rights reserved. | |
33 | * | |
34 | * Redistribution and use in source and binary forms, with or without | |
35 | * modification, are permitted provided that the following conditions | |
36 | * are met: | |
37 | * 1. Redistributions of source code must retain the above copyright | |
38 | * notice, this list of conditions and the following disclaimer. | |
39 | * 2. Redistributions in binary form must reproduce the above copyright | |
40 | * notice, this list of conditions and the following disclaimer in the | |
41 | * documentation and/or other materials provided with the distribution. | |
42 | * 3. All advertising materials mentioning features or use of this software | |
43 | * must display the following acknowledgement: | |
44 | * This product includes software developed by the University of | |
45 | * California, Berkeley and its contributors. | |
46 | * 4. Neither the name of the University nor the names of its contributors | |
47 | * may be used to endorse or promote products derived from this software | |
48 | * without specific prior written permission. | |
49 | * | |
50 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
51 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
52 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
53 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
54 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
55 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
56 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
57 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
58 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
59 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
60 | * SUCH DAMAGE. | |
61 | * | |
62 | * @(#)raw_ip.c 8.2 (Berkeley) 1/4/94 | |
63 | */ | |
64 | #if BSD310 | |
65 | #include "opt_inet.h" | |
66 | #endif | |
67 | ||
68 | #include <stddef.h> | |
69 | ||
70 | #include <sys/param.h> | |
71 | #include <sys/malloc.h> | |
72 | #include <sys/proc.h> | |
73 | #include <sys/mbuf.h> | |
74 | #include <sys/socket.h> | |
75 | #include <sys/protosw.h> | |
76 | #include <sys/socketvar.h> | |
77 | #include <sys/errno.h> | |
78 | #include <sys/systm.h> | |
79 | ||
80 | #include <net/if.h> | |
81 | #include <net/route.h> | |
82 | #include <net/if_types.h> | |
83 | ||
84 | #include <netinet/in.h> | |
85 | #include <netinet/in_var.h> | |
86 | #include <netinet/in_systm.h> | |
87 | #include <netinet/ip6.h> | |
88 | #include <netinet6/ip6_var.h> | |
89 | #include <netinet6/ip6_mroute.h> | |
90 | #include <netinet/icmp6.h> | |
91 | #if defined(__FreeBSD__) && __FreeBSD__ >= 3 || defined (__APPLE__) | |
92 | #include <netinet/in_pcb.h> | |
93 | #endif | |
94 | #include <netinet6/in6_pcb.h> | |
95 | #include <netinet6/nd6.h> | |
96 | #include <netinet6/ip6protosw.h> | |
97 | ||
98 | #if IPSEC | |
99 | #include <netinet6/ipsec.h> | |
100 | #endif /*IPSEC*/ | |
101 | ||
102 | ||
103 | #include "faith.h" | |
104 | ||
105 | #define satosin6(sa) ((struct sockaddr_in6 *)(sa)) | |
106 | #define ifatoia6(ifa) ((struct in6_ifaddr *)(ifa)) | |
107 | ||
108 | /* | |
109 | * Raw interface to IP6 protocol. | |
110 | */ | |
111 | ||
112 | extern struct inpcbhead ripcb; | |
113 | extern struct inpcbinfo ripcbinfo; | |
114 | extern u_long rip_sendspace; | |
115 | extern u_long rip_recvspace; | |
116 | ||
117 | /* | |
118 | * Setup generic address and protocol structures | |
119 | * for raw_input routine, then pass them along with | |
120 | * mbuf chain. | |
121 | */ | |
122 | int | |
123 | rip6_input(mp, offp, proto) | |
124 | struct mbuf **mp; | |
125 | int *offp, proto; | |
126 | { | |
127 | struct mbuf *m = *mp; | |
128 | register struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); | |
129 | register struct inpcb *in6p; | |
130 | struct inpcb *last = 0; | |
131 | struct ip6_recvpktopts opts; | |
132 | struct sockaddr_in6 rip6src; | |
133 | ||
134 | #if defined(NFAITH) && 0 < NFAITH | |
135 | if (m->m_pkthdr.rcvif) { | |
136 | if (m->m_pkthdr.rcvif->if_type == IFT_FAITH) { | |
137 | /* XXX send icmp6 host/port unreach? */ | |
138 | m_freem(m); | |
139 | return IPPROTO_DONE; | |
140 | } | |
141 | } | |
142 | #endif | |
143 | init_sin6(&rip6src, m); /* general init */ | |
144 | bzero(&opts, sizeof(opts)); | |
145 | ||
146 | LIST_FOREACH(in6p, &ripcb, inp_list) { | |
147 | if ((in6p->in6p_vflag & INP_IPV6) == NULL) | |
148 | continue; | |
149 | if (in6p->in6p_ip6_nxt && | |
150 | in6p->in6p_ip6_nxt != proto) | |
151 | continue; | |
152 | if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) && | |
153 | !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst)) | |
154 | continue; | |
155 | if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) && | |
156 | !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src)) | |
157 | continue; | |
158 | if (in6p->in6p_cksum != -1 | |
159 | && in6_cksum(m, ip6->ip6_nxt, *offp, | |
160 | m->m_pkthdr.len - *offp)) { | |
161 | /* XXX bark something */ | |
162 | continue; | |
163 | } | |
164 | if (last) { | |
165 | struct mbuf *n = m_copy(m, 0, (int)M_COPYALL); | |
166 | if (n) { | |
167 | if (last->in6p_flags & IN6P_CONTROLOPTS || | |
168 | last->in6p_socket->so_options & SO_TIMESTAMP) | |
169 | ip6_savecontrol(last, ip6, n, &opts, | |
170 | NULL); | |
171 | /* strip intermediate headers */ | |
172 | m_adj(n, *offp); | |
173 | if (sbappendaddr(&last->in6p_socket->so_rcv, | |
174 | (struct sockaddr *)&rip6src, | |
175 | n, opts.head) == 0) { | |
176 | /* should notify about lost packet */ | |
177 | m_freem(n); | |
178 | if (opts.head) | |
179 | m_freem(opts.head); | |
180 | } else | |
181 | sorwakeup(last->in6p_socket); | |
182 | bzero(&opts, sizeof(opts)); | |
183 | } | |
184 | } | |
185 | last = in6p; | |
186 | } | |
187 | if (last) { | |
188 | if (last->in6p_flags & IN6P_CONTROLOPTS || | |
189 | last->in6p_socket->so_options & SO_TIMESTAMP) | |
190 | ip6_savecontrol(last, ip6, m, &opts, NULL); | |
191 | /* strip intermediate headers */ | |
192 | m_adj(m, *offp); | |
193 | if (sbappendaddr(&last->in6p_socket->so_rcv, | |
194 | (struct sockaddr *)&rip6src, m, opts.head) == 0) { | |
195 | m_freem(m); | |
196 | if (opts.head) | |
197 | m_freem(opts.head); | |
198 | } else | |
199 | sorwakeup(last->in6p_socket); | |
200 | } else { | |
201 | if (proto == IPPROTO_NONE) | |
202 | m_freem(m); | |
203 | else { | |
204 | char *prvnxtp = ip6_get_prevhdr(m, *offp); /* XXX */ | |
205 | icmp6_error(m, ICMP6_PARAM_PROB, | |
206 | ICMP6_PARAMPROB_NEXTHEADER, | |
207 | prvnxtp - mtod(m, char *)); | |
208 | } | |
209 | ip6stat.ip6s_delivered--; | |
210 | } | |
211 | return IPPROTO_DONE; | |
212 | } | |
213 | ||
214 | void | |
215 | rip6_ctlinput(cmd, sa, d) | |
216 | int cmd; | |
217 | struct sockaddr *sa; | |
218 | void *d; | |
219 | { | |
220 | struct sockaddr_in6 sa6; | |
221 | struct ip6_hdr *ip6; | |
222 | struct mbuf *m; | |
223 | int off = 0; | |
224 | void (*notify) __P((struct inpcb *, int)) = in6_rtchange; | |
225 | ||
226 | if (sa->sa_family != AF_INET6 || | |
227 | sa->sa_len != sizeof(struct sockaddr_in6)) | |
228 | return; | |
229 | ||
230 | if ((unsigned)cmd >= PRC_NCMDS) | |
231 | return; | |
232 | if (PRC_IS_REDIRECT(cmd)) | |
233 | notify = in6_rtchange, d = NULL; | |
234 | else if (cmd == PRC_HOSTDEAD) | |
235 | d = NULL; | |
236 | else if (inet6ctlerrmap[cmd] == 0) | |
237 | return; | |
238 | ||
239 | /* if the parameter is from icmp6, decode it. */ | |
240 | if (d != NULL) { | |
241 | struct ip6ctlparam *ip6cp = (struct ip6ctlparam *)d; | |
242 | m = ip6cp->ip6c_m; | |
243 | ip6 = ip6cp->ip6c_ip6; | |
244 | off = ip6cp->ip6c_off; | |
245 | } else { | |
246 | m = NULL; | |
247 | ip6 = NULL; | |
248 | } | |
249 | ||
250 | /* translate addresses into internal form */ | |
251 | sa6 = *(struct sockaddr_in6 *)sa; | |
252 | if (IN6_IS_ADDR_LINKLOCAL(&sa6.sin6_addr) && m && m->m_pkthdr.rcvif) | |
253 | sa6.sin6_addr.s6_addr16[1] = htons(m->m_pkthdr.rcvif->if_index); | |
254 | ||
255 | if (ip6) { | |
256 | /* | |
257 | * XXX: We assume that when IPV6 is non NULL, | |
258 | * M and OFF are valid. | |
259 | */ | |
260 | struct in6_addr s; | |
261 | ||
262 | /* translate addresses into internal form */ | |
263 | memcpy(&s, &ip6->ip6_src, sizeof(s)); | |
264 | if (IN6_IS_ADDR_LINKLOCAL(&s)) | |
265 | s.s6_addr16[1] = htons(m->m_pkthdr.rcvif->if_index); | |
266 | ||
267 | (void) in6_pcbnotify(&ripcb, (struct sockaddr *)&sa6, | |
268 | 0, &s, 0, cmd, notify); | |
269 | } else | |
270 | (void) in6_pcbnotify(&ripcb, (struct sockaddr *)&sa6, 0, | |
271 | &zeroin6_addr, 0, cmd, notify); | |
272 | } | |
273 | ||
274 | /* | |
275 | * Generate IPv6 header and pass packet to ip6_output. | |
276 | * Tack on options user may have setup with control call. | |
277 | */ | |
278 | int | |
279 | rip6_output(m, so, dstsock, control) | |
280 | register struct mbuf *m; | |
281 | struct socket *so; | |
282 | struct sockaddr_in6 *dstsock; | |
283 | struct mbuf *control; | |
284 | { | |
285 | struct in6_addr *dst; | |
286 | struct ip6_hdr *ip6; | |
287 | struct inpcb *in6p; | |
288 | u_int plen = m->m_pkthdr.len; | |
289 | int error = 0; | |
290 | struct ip6_pktopts opt, *optp = 0; | |
291 | struct ifnet *oifp = NULL; | |
292 | int type = 0, code = 0; /* for ICMPv6 output statistics only */ | |
293 | int priv = 0; | |
294 | ||
295 | in6p = sotoin6pcb(so); | |
296 | ||
297 | priv = 0; | |
298 | #if !defined(__APPLE__) | |
299 | { | |
300 | struct proc *p = current_proc(); /* XXX */ | |
301 | ||
302 | if (p && !suser(p->p_ucred, &p->p_acflag)) | |
303 | priv = 1; | |
304 | } | |
305 | #else | |
306 | if ((so->so_state & SS_PRIV) != 0) | |
307 | priv = 1; | |
308 | #endif | |
309 | dst = &dstsock->sin6_addr; | |
310 | if (control) { | |
311 | if ((error = ip6_setpktoptions(control, &opt, priv, 0)) != 0) | |
312 | goto bad; | |
313 | optp = &opt; | |
314 | } else | |
315 | optp = in6p->in6p_outputopts; | |
316 | ||
317 | /* | |
318 | * For an ICMPv6 packet, we should know its type and code | |
319 | * to update statistics. | |
320 | */ | |
321 | if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) { | |
322 | struct icmp6_hdr *icmp6; | |
323 | if (m->m_len < sizeof(struct icmp6_hdr) && | |
324 | (m = m_pullup(m, sizeof(struct icmp6_hdr))) == NULL) { | |
325 | error = ENOBUFS; | |
326 | goto bad; | |
327 | } | |
328 | icmp6 = mtod(m, struct icmp6_hdr *); | |
329 | type = icmp6->icmp6_type; | |
330 | code = icmp6->icmp6_code; | |
331 | } | |
332 | ||
333 | M_PREPEND(m, sizeof(*ip6), M_WAIT); | |
334 | ip6 = mtod(m, struct ip6_hdr *); | |
335 | ||
336 | /* | |
337 | * Next header might not be ICMP6 but use its pseudo header anyway. | |
338 | */ | |
339 | ip6->ip6_dst = *dst; | |
340 | ||
341 | /* | |
342 | * If the scope of the destination is link-local, embed the interface | |
343 | * index in the address. | |
344 | * | |
345 | * XXX advanced-api value overrides sin6_scope_id | |
346 | */ | |
347 | if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) { | |
348 | struct in6_pktinfo *pi; | |
349 | ||
350 | /* | |
351 | * XXX Boundary check is assumed to be already done in | |
352 | * ip6_setpktoptions(). | |
353 | */ | |
354 | if (optp && (pi = optp->ip6po_pktinfo) && pi->ipi6_ifindex) { | |
355 | ip6->ip6_dst.s6_addr16[1] = htons(pi->ipi6_ifindex); | |
356 | oifp = ifindex2ifnet[pi->ipi6_ifindex]; | |
357 | } | |
358 | else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) && | |
359 | in6p->in6p_moptions && | |
360 | in6p->in6p_moptions->im6o_multicast_ifp) { | |
361 | oifp = in6p->in6p_moptions->im6o_multicast_ifp; | |
362 | ip6->ip6_dst.s6_addr16[1] = htons(oifp->if_index); | |
363 | } else if (dstsock->sin6_scope_id) { | |
364 | /* boundary check */ | |
365 | if (dstsock->sin6_scope_id < 0 | |
366 | || if_index < dstsock->sin6_scope_id) { | |
367 | error = ENXIO; /* XXX EINVAL? */ | |
368 | goto bad; | |
369 | } | |
370 | ip6->ip6_dst.s6_addr16[1] | |
371 | = htons(dstsock->sin6_scope_id & 0xffff);/*XXX*/ | |
372 | } | |
373 | } | |
374 | ||
375 | /* | |
376 | * Source address selection. | |
377 | */ | |
378 | { | |
379 | struct in6_addr *in6a; | |
380 | ||
381 | if ((in6a = in6_selectsrc(dstsock, optp, | |
382 | in6p->in6p_moptions, | |
383 | &in6p->in6p_route, | |
384 | &in6p->in6p_laddr, | |
385 | &error)) == 0) { | |
386 | if (error == 0) | |
387 | error = EADDRNOTAVAIL; | |
388 | goto bad; | |
389 | } | |
390 | ip6->ip6_src = *in6a; | |
391 | if (in6p->in6p_route.ro_rt) | |
392 | oifp = ifindex2ifnet[in6p->in6p_route.ro_rt->rt_ifp->if_index]; | |
393 | } | |
394 | ||
395 | ip6->ip6_flow = in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK; | |
396 | ip6->ip6_vfc |= IPV6_VERSION; | |
397 | #if 0 /* ip6_plen will be filled in ip6_output. */ | |
398 | ip6->ip6_plen = htons((u_short)plen); | |
399 | #endif | |
400 | ip6->ip6_nxt = in6p->in6p_ip6_nxt; | |
401 | ip6->ip6_hlim = in6_selecthlim(in6p, oifp); | |
402 | ||
403 | if (so->so_proto->pr_protocol == IPPROTO_ICMPV6 || | |
404 | in6p->in6p_cksum != -1) { | |
405 | struct mbuf *n; | |
406 | int off; | |
407 | u_int16_t *p; | |
408 | ||
409 | #define offsetof(type, member) ((size_t)(&((type *)0)->member)) /* XXX */ | |
410 | ||
411 | /* compute checksum */ | |
412 | if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) | |
413 | off = offsetof(struct icmp6_hdr, icmp6_cksum); | |
414 | else | |
415 | off = in6p->in6p_cksum; | |
416 | if (plen < off + 1) { | |
417 | error = EINVAL; | |
418 | goto bad; | |
419 | } | |
420 | off += sizeof(struct ip6_hdr); | |
421 | ||
422 | n = m; | |
423 | while (n && n->m_len <= off) { | |
424 | off -= n->m_len; | |
425 | n = n->m_next; | |
426 | } | |
427 | if (!n) | |
428 | goto bad; | |
429 | p = (u_int16_t *)(mtod(n, caddr_t) + off); | |
430 | *p = 0; | |
431 | *p = in6_cksum(m, ip6->ip6_nxt, sizeof(*ip6), plen); | |
432 | } | |
433 | ||
434 | #if IPSEC | |
435 | ipsec_setsocket(m, so); | |
436 | #endif /*IPSEC*/ | |
437 | ||
438 | error = ip6_output(m, optp, &in6p->in6p_route, 0, in6p->in6p_moptions, | |
439 | &oifp); | |
440 | if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) { | |
441 | if (oifp) | |
442 | icmp6_ifoutstat_inc(oifp, type, code); | |
443 | icmp6stat.icp6s_outhist[type]++; | |
444 | } | |
445 | ||
446 | goto freectl; | |
447 | ||
448 | bad: | |
449 | if (m) | |
450 | m_freem(m); | |
451 | ||
452 | freectl: | |
453 | if (optp == &opt && optp->ip6po_rthdr && optp->ip6po_route.ro_rt) | |
454 | RTFREE(optp->ip6po_route.ro_rt); | |
455 | if (control) { | |
456 | if (optp == &opt) | |
457 | ip6_clearpktopts(optp, 0, -1); | |
458 | m_freem(control); | |
459 | } | |
460 | return(error); | |
461 | } | |
462 | ||
463 | /* | |
464 | * Raw IPv6 socket option processing. | |
465 | */ | |
466 | int | |
467 | rip6_ctloutput(so, sopt) | |
468 | struct socket *so; | |
469 | struct sockopt *sopt; | |
470 | { | |
471 | int error; | |
472 | ||
473 | if (sopt->sopt_level == IPPROTO_ICMPV6) | |
474 | /* | |
475 | * XXX: is it better to call icmp6_ctloutput() directly | |
476 | * from protosw? | |
477 | */ | |
478 | return(icmp6_ctloutput(so, sopt)); | |
479 | else if (sopt->sopt_level != IPPROTO_IPV6) | |
480 | return (EINVAL); | |
481 | ||
482 | error = 0; | |
483 | ||
484 | switch (sopt->sopt_dir) { | |
485 | case SOPT_GET: | |
486 | switch (sopt->sopt_name) { | |
487 | case MRT6_INIT: | |
488 | case MRT6_DONE: | |
489 | case MRT6_ADD_MIF: | |
490 | case MRT6_DEL_MIF: | |
491 | case MRT6_ADD_MFC: | |
492 | case MRT6_DEL_MFC: | |
493 | case MRT6_PIM: | |
494 | error = ip6_mrouter_get(so, sopt); | |
495 | break; | |
496 | default: | |
497 | error = ip6_ctloutput(so, sopt); | |
498 | break; | |
499 | } | |
500 | break; | |
501 | ||
502 | case SOPT_SET: | |
503 | switch (sopt->sopt_name) { | |
504 | case MRT6_INIT: | |
505 | case MRT6_DONE: | |
506 | case MRT6_ADD_MIF: | |
507 | case MRT6_DEL_MIF: | |
508 | case MRT6_ADD_MFC: | |
509 | case MRT6_DEL_MFC: | |
510 | case MRT6_PIM: | |
511 | error = ip6_mrouter_set(so, sopt); | |
512 | break; | |
513 | default: | |
514 | error = ip6_ctloutput(so, sopt); | |
515 | break; | |
516 | } | |
517 | break; | |
518 | } | |
519 | ||
520 | return (error); | |
521 | } | |
522 | ||
523 | static int | |
524 | rip6_attach(struct socket *so, int proto, struct proc *p) | |
525 | { | |
526 | struct inpcb *inp; | |
527 | int error, s; | |
528 | ||
529 | inp = sotoinpcb(so); | |
530 | if (inp) | |
531 | panic("rip6_attach"); | |
532 | if (p && (error = suser(p->p_ucred, &p->p_acflag)) != 0) | |
533 | return error; | |
534 | ||
535 | if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { | |
536 | error = soreserve(so, rip_sendspace, rip_recvspace); | |
537 | if (error) | |
538 | return error; | |
539 | } | |
540 | s = splnet(); | |
541 | error = in_pcballoc(so, &ripcbinfo, p); | |
542 | splx(s); | |
543 | if (error) | |
544 | return error; | |
545 | inp = (struct inpcb *)so->so_pcb; | |
546 | inp->inp_vflag |= INP_IPV6; | |
547 | inp->in6p_ip6_nxt = (long)proto; | |
548 | inp->in6p_hops = -1; /* use kernel default */ | |
549 | inp->in6p_cksum = -1; | |
550 | #if IPSEC | |
551 | error = ipsec_init_policy(so, &inp->in6p_sp); | |
552 | if (error != 0) { | |
553 | in6_pcbdetach(inp); | |
554 | return (error); | |
555 | } | |
556 | #endif /*IPSEC*/ | |
557 | MALLOC(inp->in6p_icmp6filt, struct icmp6_filter *, | |
558 | sizeof(struct icmp6_filter), M_PCB, M_NOWAIT); | |
0b4e3aa0 A |
559 | |
560 | if (inp->in6p_icmp6filt == NULL) | |
561 | return(ENOBUFS); | |
562 | ||
1c79356b A |
563 | ICMP6_FILTER_SETPASSALL(inp->in6p_icmp6filt); |
564 | return 0; | |
565 | } | |
566 | ||
567 | static int | |
568 | rip6_detach(struct socket *so) | |
569 | { | |
570 | struct inpcb *inp; | |
571 | ||
572 | inp = sotoinpcb(so); | |
573 | if (inp == 0) | |
574 | panic("rip6_detach"); | |
575 | if (so == ip6_mrouter) | |
576 | ip6_mrouter_done(); | |
577 | /* xxx: RSVP */ | |
578 | if (inp->in6p_icmp6filt) { | |
579 | FREE(inp->in6p_icmp6filt, M_PCB); | |
580 | inp->in6p_icmp6filt = NULL; | |
581 | } | |
582 | in6_pcbdetach(inp); | |
583 | return 0; | |
584 | } | |
585 | ||
586 | static int | |
587 | rip6_abort(struct socket *so) | |
588 | { | |
589 | soisdisconnected(so); | |
590 | return rip6_detach(so); | |
591 | } | |
592 | ||
593 | static int | |
594 | rip6_disconnect(struct socket *so) | |
595 | { | |
596 | struct inpcb *inp = sotoinpcb(so); | |
597 | ||
598 | if ((so->so_state & SS_ISCONNECTED) == 0) | |
599 | return ENOTCONN; | |
600 | inp->in6p_faddr = in6addr_any; | |
601 | return rip6_abort(so); | |
602 | } | |
603 | ||
604 | static int | |
605 | rip6_bind(struct socket *so, struct sockaddr *nam, struct proc *p) | |
606 | { | |
607 | struct inpcb *inp = sotoinpcb(so); | |
608 | struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam; | |
609 | struct ifaddr *ia = NULL; | |
610 | ||
611 | if (nam->sa_len != sizeof(*addr)) | |
612 | return EINVAL; | |
613 | ||
614 | if (TAILQ_EMPTY(&ifnet) || addr->sin6_family != AF_INET6) | |
615 | return EADDRNOTAVAIL; | |
616 | if (!IN6_IS_ADDR_UNSPECIFIED(&addr->sin6_addr) && | |
617 | (ia = ifa_ifwithaddr((struct sockaddr *)addr)) == 0) | |
618 | return EADDRNOTAVAIL; | |
619 | if (ia && | |
620 | ((struct in6_ifaddr *)ia)->ia6_flags & | |
621 | (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY| | |
622 | IN6_IFF_DETACHED|IN6_IFF_DEPRECATED)) { | |
623 | return(EADDRNOTAVAIL); | |
624 | } | |
625 | inp->in6p_laddr = addr->sin6_addr; | |
626 | return 0; | |
627 | } | |
628 | ||
629 | static int | |
630 | rip6_connect(struct socket *so, struct sockaddr *nam, struct proc *p) | |
631 | { | |
632 | struct inpcb *inp = sotoinpcb(so); | |
633 | struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam; | |
634 | struct in6_addr *in6a = NULL; | |
635 | int error = 0; | |
636 | ||
637 | if (nam->sa_len != sizeof(*addr)) | |
638 | return EINVAL; | |
639 | if (TAILQ_EMPTY(&ifnet)) | |
640 | return EADDRNOTAVAIL; | |
641 | if (addr->sin6_family != AF_INET6) | |
642 | return EAFNOSUPPORT; | |
643 | ||
644 | /* Source address selection. XXX: need pcblookup? */ | |
645 | in6a = in6_selectsrc(addr, inp->in6p_outputopts, | |
646 | inp->in6p_moptions, &inp->in6p_route, | |
647 | &inp->in6p_laddr, &error); | |
648 | if (in6a == NULL) | |
649 | return (error ? error : EADDRNOTAVAIL); | |
650 | inp->in6p_laddr = *in6a; | |
651 | inp->in6p_faddr = addr->sin6_addr; | |
652 | soisconnected(so); | |
653 | return 0; | |
654 | } | |
655 | ||
656 | static int | |
657 | rip6_shutdown(struct socket *so) | |
658 | { | |
659 | socantsendmore(so); | |
660 | return 0; | |
661 | } | |
662 | ||
663 | static int | |
664 | rip6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, | |
665 | struct mbuf *control, struct proc *p) | |
666 | { | |
667 | struct inpcb *inp = sotoinpcb(so); | |
668 | struct sockaddr_in6 tmp; | |
669 | struct sockaddr_in6 *dst; | |
670 | ||
671 | if (so->so_state & SS_ISCONNECTED) { | |
672 | if (nam) { | |
673 | m_freem(m); | |
674 | return EISCONN; | |
675 | } | |
676 | /* XXX */ | |
677 | bzero(&tmp, sizeof(tmp)); | |
678 | tmp.sin6_family = AF_INET6; | |
679 | tmp.sin6_len = sizeof(struct sockaddr_in6); | |
680 | bcopy(&inp->in6p_faddr, &tmp.sin6_addr, | |
681 | sizeof(struct in6_addr)); | |
682 | dst = &tmp; | |
683 | } else { | |
684 | if (nam == NULL) { | |
685 | m_freem(m); | |
686 | return ENOTCONN; | |
687 | } | |
688 | dst = (struct sockaddr_in6 *)nam; | |
689 | } | |
690 | return rip6_output(m, so, dst, control); | |
691 | } | |
692 | ||
693 | struct pr_usrreqs rip6_usrreqs = { | |
694 | rip6_abort, pru_accept_notsupp, rip6_attach, rip6_bind, rip6_connect, | |
695 | pru_connect2_notsupp, in6_control, rip6_detach, rip6_disconnect, | |
696 | pru_listen_notsupp, in6_setpeeraddr, pru_rcvd_notsupp, | |
697 | pru_rcvoob_notsupp, rip6_send, pru_sense_null, rip6_shutdown, | |
698 | in6_setsockaddr, sosend, soreceive, sopoll | |
699 | }; |