neighbor free after disconnect
[cor_2_6_31.git] / net / ipv4 / ip_sockglue.c
blobfc7993e9061fd855d1cb34b42fa491c49dcd669d
1 /*
2 * INET An implementation of the TCP/IP protocol suite for the LINUX
3 * operating system. INET is implemented using the BSD Socket
4 * interface as the means of communication with the user level.
6 * The IP to API glue.
8 * Authors: see ip.c
10 * Fixes:
11 * Many : Split from ip.c , see ip.c for history.
12 * Martin Mares : TOS setting fixed.
13 * Alan Cox : Fixed a couple of oopses in Martin's
14 * TOS tweaks.
15 * Mike McLagan : Routing by source
18 #include <linux/module.h>
19 #include <linux/types.h>
20 #include <linux/mm.h>
21 #include <linux/skbuff.h>
22 #include <linux/ip.h>
23 #include <linux/icmp.h>
24 #include <linux/inetdevice.h>
25 #include <linux/netdevice.h>
26 #include <net/sock.h>
27 #include <net/ip.h>
28 #include <net/icmp.h>
29 #include <net/tcp_states.h>
30 #include <linux/udp.h>
31 #include <linux/igmp.h>
32 #include <linux/netfilter.h>
33 #include <linux/route.h>
34 #include <linux/mroute.h>
35 #include <net/route.h>
36 #include <net/xfrm.h>
37 #include <net/compat.h>
38 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
39 #include <net/transp_v6.h>
40 #endif
42 #include <linux/errqueue.h>
43 #include <asm/uaccess.h>
45 #define IP_CMSG_PKTINFO 1
46 #define IP_CMSG_TTL 2
47 #define IP_CMSG_TOS 4
48 #define IP_CMSG_RECVOPTS 8
49 #define IP_CMSG_RETOPTS 16
50 #define IP_CMSG_PASSSEC 32
51 #define IP_CMSG_ORIGDSTADDR 64
54 * SOL_IP control messages.
57 static void ip_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb)
59 struct in_pktinfo info;
60 struct rtable *rt = skb_rtable(skb);
62 info.ipi_addr.s_addr = ip_hdr(skb)->daddr;
63 if (rt) {
64 info.ipi_ifindex = rt->rt_iif;
65 info.ipi_spec_dst.s_addr = rt->rt_spec_dst;
66 } else {
67 info.ipi_ifindex = 0;
68 info.ipi_spec_dst.s_addr = 0;
71 put_cmsg(msg, SOL_IP, IP_PKTINFO, sizeof(info), &info);
74 static void ip_cmsg_recv_ttl(struct msghdr *msg, struct sk_buff *skb)
76 int ttl = ip_hdr(skb)->ttl;
77 put_cmsg(msg, SOL_IP, IP_TTL, sizeof(int), &ttl);
80 static void ip_cmsg_recv_tos(struct msghdr *msg, struct sk_buff *skb)
82 put_cmsg(msg, SOL_IP, IP_TOS, 1, &ip_hdr(skb)->tos);
85 static void ip_cmsg_recv_opts(struct msghdr *msg, struct sk_buff *skb)
87 if (IPCB(skb)->opt.optlen == 0)
88 return;
90 put_cmsg(msg, SOL_IP, IP_RECVOPTS, IPCB(skb)->opt.optlen,
91 ip_hdr(skb) + 1);
95 static void ip_cmsg_recv_retopts(struct msghdr *msg, struct sk_buff *skb)
97 unsigned char optbuf[sizeof(struct ip_options) + 40];
98 struct ip_options * opt = (struct ip_options *)optbuf;
100 if (IPCB(skb)->opt.optlen == 0)
101 return;
103 if (ip_options_echo(opt, skb)) {
104 msg->msg_flags |= MSG_CTRUNC;
105 return;
107 ip_options_undo(opt);
109 put_cmsg(msg, SOL_IP, IP_RETOPTS, opt->optlen, opt->__data);
112 static void ip_cmsg_recv_security(struct msghdr *msg, struct sk_buff *skb)
114 char *secdata;
115 u32 seclen, secid;
116 int err;
118 err = security_socket_getpeersec_dgram(NULL, skb, &secid);
119 if (err)
120 return;
122 err = security_secid_to_secctx(secid, &secdata, &seclen);
123 if (err)
124 return;
126 put_cmsg(msg, SOL_IP, SCM_SECURITY, seclen, secdata);
127 security_release_secctx(secdata, seclen);
130 static void ip_cmsg_recv_dstaddr(struct msghdr *msg, struct sk_buff *skb)
132 struct sockaddr_in sin;
133 struct iphdr *iph = ip_hdr(skb);
134 __be16 *ports = (__be16 *)skb_transport_header(skb);
136 if (skb_transport_offset(skb) + 4 > skb->len)
137 return;
139 /* All current transport protocols have the port numbers in the
140 * first four bytes of the transport header and this function is
141 * written with this assumption in mind.
144 sin.sin_family = AF_INET;
145 sin.sin_addr.s_addr = iph->daddr;
146 sin.sin_port = ports[1];
147 memset(sin.sin_zero, 0, sizeof(sin.sin_zero));
149 put_cmsg(msg, SOL_IP, IP_ORIGDSTADDR, sizeof(sin), &sin);
152 void ip_cmsg_recv(struct msghdr *msg, struct sk_buff *skb)
154 struct inet_sock *inet = inet_sk(skb->sk);
155 unsigned flags = inet->cmsg_flags;
157 /* Ordered by supposed usage frequency */
158 if (flags & 1)
159 ip_cmsg_recv_pktinfo(msg, skb);
160 if ((flags >>= 1) == 0)
161 return;
163 if (flags & 1)
164 ip_cmsg_recv_ttl(msg, skb);
165 if ((flags >>= 1) == 0)
166 return;
168 if (flags & 1)
169 ip_cmsg_recv_tos(msg, skb);
170 if ((flags >>= 1) == 0)
171 return;
173 if (flags & 1)
174 ip_cmsg_recv_opts(msg, skb);
175 if ((flags >>= 1) == 0)
176 return;
178 if (flags & 1)
179 ip_cmsg_recv_retopts(msg, skb);
180 if ((flags >>= 1) == 0)
181 return;
183 if (flags & 1)
184 ip_cmsg_recv_security(msg, skb);
186 if ((flags >>= 1) == 0)
187 return;
188 if (flags & 1)
189 ip_cmsg_recv_dstaddr(msg, skb);
192 EXPORT_SYMBOL(ip_cmsg_recv);
194 int ip_cmsg_send(struct net *net, struct msghdr *msg, struct ipcm_cookie *ipc)
196 int err;
197 struct cmsghdr *cmsg;
199 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
200 if (!CMSG_OK(msg, cmsg))
201 return -EINVAL;
202 if (cmsg->cmsg_level != SOL_IP)
203 continue;
204 switch (cmsg->cmsg_type) {
205 case IP_RETOPTS:
206 err = cmsg->cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr));
207 err = ip_options_get(net, &ipc->opt, CMSG_DATA(cmsg),
208 err < 40 ? err : 40);
209 if (err)
210 return err;
211 break;
212 case IP_PKTINFO:
214 struct in_pktinfo *info;
215 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct in_pktinfo)))
216 return -EINVAL;
217 info = (struct in_pktinfo *)CMSG_DATA(cmsg);
218 ipc->oif = info->ipi_ifindex;
219 ipc->addr = info->ipi_spec_dst.s_addr;
220 break;
222 default:
223 return -EINVAL;
226 return 0;
230 /* Special input handler for packets caught by router alert option.
231 They are selected only by protocol field, and then processed likely
232 local ones; but only if someone wants them! Otherwise, router
233 not running rsvpd will kill RSVP.
235 It is user level problem, what it will make with them.
236 I have no idea, how it will masquearde or NAT them (it is joke, joke :-)),
237 but receiver should be enough clever f.e. to forward mtrace requests,
238 sent to multicast group to reach destination designated router.
240 struct ip_ra_chain *ip_ra_chain;
241 DEFINE_RWLOCK(ip_ra_lock);
243 int ip_ra_control(struct sock *sk, unsigned char on,
244 void (*destructor)(struct sock *))
246 struct ip_ra_chain *ra, *new_ra, **rap;
248 if (sk->sk_type != SOCK_RAW || inet_sk(sk)->num == IPPROTO_RAW)
249 return -EINVAL;
251 new_ra = on ? kmalloc(sizeof(*new_ra), GFP_KERNEL) : NULL;
253 write_lock_bh(&ip_ra_lock);
254 for (rap = &ip_ra_chain; (ra = *rap) != NULL; rap = &ra->next) {
255 if (ra->sk == sk) {
256 if (on) {
257 write_unlock_bh(&ip_ra_lock);
258 kfree(new_ra);
259 return -EADDRINUSE;
261 *rap = ra->next;
262 write_unlock_bh(&ip_ra_lock);
264 if (ra->destructor)
265 ra->destructor(sk);
266 sock_put(sk);
267 kfree(ra);
268 return 0;
271 if (new_ra == NULL) {
272 write_unlock_bh(&ip_ra_lock);
273 return -ENOBUFS;
275 new_ra->sk = sk;
276 new_ra->destructor = destructor;
278 new_ra->next = ra;
279 *rap = new_ra;
280 sock_hold(sk);
281 write_unlock_bh(&ip_ra_lock);
283 return 0;
286 void ip_icmp_error(struct sock *sk, struct sk_buff *skb, int err,
287 __be16 port, u32 info, u8 *payload)
289 struct inet_sock *inet = inet_sk(sk);
290 struct sock_exterr_skb *serr;
292 if (!inet->recverr)
293 return;
295 skb = skb_clone(skb, GFP_ATOMIC);
296 if (!skb)
297 return;
299 serr = SKB_EXT_ERR(skb);
300 serr->ee.ee_errno = err;
301 serr->ee.ee_origin = SO_EE_ORIGIN_ICMP;
302 serr->ee.ee_type = icmp_hdr(skb)->type;
303 serr->ee.ee_code = icmp_hdr(skb)->code;
304 serr->ee.ee_pad = 0;
305 serr->ee.ee_info = info;
306 serr->ee.ee_data = 0;
307 serr->addr_offset = (u8 *)&(((struct iphdr *)(icmp_hdr(skb) + 1))->daddr) -
308 skb_network_header(skb);
309 serr->port = port;
311 if (skb_pull(skb, payload - skb->data) != NULL) {
312 skb_reset_transport_header(skb);
313 if (sock_queue_err_skb(sk, skb) == 0)
314 return;
316 kfree_skb(skb);
319 void ip_local_error(struct sock *sk, int err, __be32 daddr, __be16 port, u32 info)
321 struct inet_sock *inet = inet_sk(sk);
322 struct sock_exterr_skb *serr;
323 struct iphdr *iph;
324 struct sk_buff *skb;
326 if (!inet->recverr)
327 return;
329 skb = alloc_skb(sizeof(struct iphdr), GFP_ATOMIC);
330 if (!skb)
331 return;
333 skb_put(skb, sizeof(struct iphdr));
334 skb_reset_network_header(skb);
335 iph = ip_hdr(skb);
336 iph->daddr = daddr;
338 serr = SKB_EXT_ERR(skb);
339 serr->ee.ee_errno = err;
340 serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL;
341 serr->ee.ee_type = 0;
342 serr->ee.ee_code = 0;
343 serr->ee.ee_pad = 0;
344 serr->ee.ee_info = info;
345 serr->ee.ee_data = 0;
346 serr->addr_offset = (u8 *)&iph->daddr - skb_network_header(skb);
347 serr->port = port;
349 __skb_pull(skb, skb_tail_pointer(skb) - skb->data);
350 skb_reset_transport_header(skb);
352 if (sock_queue_err_skb(sk, skb))
353 kfree_skb(skb);
357 * Handle MSG_ERRQUEUE
359 int ip_recv_error(struct sock *sk, struct msghdr *msg, int len)
361 struct sock_exterr_skb *serr;
362 struct sk_buff *skb, *skb2;
363 struct sockaddr_in *sin;
364 struct {
365 struct sock_extended_err ee;
366 struct sockaddr_in offender;
367 } errhdr;
368 int err;
369 int copied;
371 err = -EAGAIN;
372 skb = skb_dequeue(&sk->sk_error_queue);
373 if (skb == NULL)
374 goto out;
376 copied = skb->len;
377 if (copied > len) {
378 msg->msg_flags |= MSG_TRUNC;
379 copied = len;
381 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
382 if (err)
383 goto out_free_skb;
385 sock_recv_timestamp(msg, sk, skb);
387 serr = SKB_EXT_ERR(skb);
389 sin = (struct sockaddr_in *)msg->msg_name;
390 if (sin) {
391 sin->sin_family = AF_INET;
392 sin->sin_addr.s_addr = *(__be32 *)(skb_network_header(skb) +
393 serr->addr_offset);
394 sin->sin_port = serr->port;
395 memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
398 memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err));
399 sin = &errhdr.offender;
400 sin->sin_family = AF_UNSPEC;
401 if (serr->ee.ee_origin == SO_EE_ORIGIN_ICMP) {
402 struct inet_sock *inet = inet_sk(sk);
404 sin->sin_family = AF_INET;
405 sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
406 sin->sin_port = 0;
407 memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
408 if (inet->cmsg_flags)
409 ip_cmsg_recv(msg, skb);
412 put_cmsg(msg, SOL_IP, IP_RECVERR, sizeof(errhdr), &errhdr);
414 /* Now we could try to dump offended packet options */
416 msg->msg_flags |= MSG_ERRQUEUE;
417 err = copied;
419 /* Reset and regenerate socket error */
420 spin_lock_bh(&sk->sk_error_queue.lock);
421 sk->sk_err = 0;
422 skb2 = skb_peek(&sk->sk_error_queue);
423 if (skb2 != NULL) {
424 sk->sk_err = SKB_EXT_ERR(skb2)->ee.ee_errno;
425 spin_unlock_bh(&sk->sk_error_queue.lock);
426 sk->sk_error_report(sk);
427 } else
428 spin_unlock_bh(&sk->sk_error_queue.lock);
430 out_free_skb:
431 kfree_skb(skb);
432 out:
433 return err;
438 * Socket option code for IP. This is the end of the line after any
439 * TCP,UDP etc options on an IP socket.
442 static int do_ip_setsockopt(struct sock *sk, int level,
443 int optname, char __user *optval, int optlen)
445 struct inet_sock *inet = inet_sk(sk);
446 int val = 0, err;
448 if (((1<<optname) & ((1<<IP_PKTINFO) | (1<<IP_RECVTTL) |
449 (1<<IP_RECVOPTS) | (1<<IP_RECVTOS) |
450 (1<<IP_RETOPTS) | (1<<IP_TOS) |
451 (1<<IP_TTL) | (1<<IP_HDRINCL) |
452 (1<<IP_MTU_DISCOVER) | (1<<IP_RECVERR) |
453 (1<<IP_ROUTER_ALERT) | (1<<IP_FREEBIND) |
454 (1<<IP_PASSSEC) | (1<<IP_TRANSPARENT))) ||
455 optname == IP_MULTICAST_TTL ||
456 optname == IP_MULTICAST_ALL ||
457 optname == IP_MULTICAST_LOOP ||
458 optname == IP_RECVORIGDSTADDR) {
459 if (optlen >= sizeof(int)) {
460 if (get_user(val, (int __user *) optval))
461 return -EFAULT;
462 } else if (optlen >= sizeof(char)) {
463 unsigned char ucval;
465 if (get_user(ucval, (unsigned char __user *) optval))
466 return -EFAULT;
467 val = (int) ucval;
471 /* If optlen==0, it is equivalent to val == 0 */
473 if (ip_mroute_opt(optname))
474 return ip_mroute_setsockopt(sk, optname, optval, optlen);
476 err = 0;
477 lock_sock(sk);
479 switch (optname) {
480 case IP_OPTIONS:
482 struct ip_options *opt = NULL;
483 if (optlen > 40 || optlen < 0)
484 goto e_inval;
485 err = ip_options_get_from_user(sock_net(sk), &opt,
486 optval, optlen);
487 if (err)
488 break;
489 if (inet->is_icsk) {
490 struct inet_connection_sock *icsk = inet_csk(sk);
491 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
492 if (sk->sk_family == PF_INET ||
493 (!((1 << sk->sk_state) &
494 (TCPF_LISTEN | TCPF_CLOSE)) &&
495 inet->daddr != LOOPBACK4_IPV6)) {
496 #endif
497 if (inet->opt)
498 icsk->icsk_ext_hdr_len -= inet->opt->optlen;
499 if (opt)
500 icsk->icsk_ext_hdr_len += opt->optlen;
501 icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
502 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
504 #endif
506 opt = xchg(&inet->opt, opt);
507 kfree(opt);
508 break;
510 case IP_PKTINFO:
511 if (val)
512 inet->cmsg_flags |= IP_CMSG_PKTINFO;
513 else
514 inet->cmsg_flags &= ~IP_CMSG_PKTINFO;
515 break;
516 case IP_RECVTTL:
517 if (val)
518 inet->cmsg_flags |= IP_CMSG_TTL;
519 else
520 inet->cmsg_flags &= ~IP_CMSG_TTL;
521 break;
522 case IP_RECVTOS:
523 if (val)
524 inet->cmsg_flags |= IP_CMSG_TOS;
525 else
526 inet->cmsg_flags &= ~IP_CMSG_TOS;
527 break;
528 case IP_RECVOPTS:
529 if (val)
530 inet->cmsg_flags |= IP_CMSG_RECVOPTS;
531 else
532 inet->cmsg_flags &= ~IP_CMSG_RECVOPTS;
533 break;
534 case IP_RETOPTS:
535 if (val)
536 inet->cmsg_flags |= IP_CMSG_RETOPTS;
537 else
538 inet->cmsg_flags &= ~IP_CMSG_RETOPTS;
539 break;
540 case IP_PASSSEC:
541 if (val)
542 inet->cmsg_flags |= IP_CMSG_PASSSEC;
543 else
544 inet->cmsg_flags &= ~IP_CMSG_PASSSEC;
545 break;
546 case IP_RECVORIGDSTADDR:
547 if (val)
548 inet->cmsg_flags |= IP_CMSG_ORIGDSTADDR;
549 else
550 inet->cmsg_flags &= ~IP_CMSG_ORIGDSTADDR;
551 break;
552 case IP_TOS: /* This sets both TOS and Precedence */
553 if (sk->sk_type == SOCK_STREAM) {
554 val &= ~3;
555 val |= inet->tos & 3;
557 if (inet->tos != val) {
558 inet->tos = val;
559 sk->sk_priority = rt_tos2priority(val);
560 sk_dst_reset(sk);
562 break;
563 case IP_TTL:
564 if (optlen < 1)
565 goto e_inval;
566 if (val != -1 && (val < 0 || val > 255))
567 goto e_inval;
568 inet->uc_ttl = val;
569 break;
570 case IP_HDRINCL:
571 if (sk->sk_type != SOCK_RAW) {
572 err = -ENOPROTOOPT;
573 break;
575 inet->hdrincl = val ? 1 : 0;
576 break;
577 case IP_MTU_DISCOVER:
578 if (val < 0 || val > 3)
579 goto e_inval;
580 inet->pmtudisc = val;
581 break;
582 case IP_RECVERR:
583 inet->recverr = !!val;
584 if (!val)
585 skb_queue_purge(&sk->sk_error_queue);
586 break;
587 case IP_MULTICAST_TTL:
588 if (sk->sk_type == SOCK_STREAM)
589 goto e_inval;
590 if (optlen < 1)
591 goto e_inval;
592 if (val == -1)
593 val = 1;
594 if (val < 0 || val > 255)
595 goto e_inval;
596 inet->mc_ttl = val;
597 break;
598 case IP_MULTICAST_LOOP:
599 if (optlen < 1)
600 goto e_inval;
601 inet->mc_loop = !!val;
602 break;
603 case IP_MULTICAST_IF:
605 struct ip_mreqn mreq;
606 struct net_device *dev = NULL;
608 if (sk->sk_type == SOCK_STREAM)
609 goto e_inval;
611 * Check the arguments are allowable
614 err = -EFAULT;
615 if (optlen >= sizeof(struct ip_mreqn)) {
616 if (copy_from_user(&mreq, optval, sizeof(mreq)))
617 break;
618 } else {
619 memset(&mreq, 0, sizeof(mreq));
620 if (optlen >= sizeof(struct in_addr) &&
621 copy_from_user(&mreq.imr_address, optval,
622 sizeof(struct in_addr)))
623 break;
626 if (!mreq.imr_ifindex) {
627 if (mreq.imr_address.s_addr == htonl(INADDR_ANY)) {
628 inet->mc_index = 0;
629 inet->mc_addr = 0;
630 err = 0;
631 break;
633 dev = ip_dev_find(sock_net(sk), mreq.imr_address.s_addr);
634 if (dev) {
635 mreq.imr_ifindex = dev->ifindex;
636 dev_put(dev);
638 } else
639 dev = __dev_get_by_index(sock_net(sk), mreq.imr_ifindex);
642 err = -EADDRNOTAVAIL;
643 if (!dev)
644 break;
646 err = -EINVAL;
647 if (sk->sk_bound_dev_if &&
648 mreq.imr_ifindex != sk->sk_bound_dev_if)
649 break;
651 inet->mc_index = mreq.imr_ifindex;
652 inet->mc_addr = mreq.imr_address.s_addr;
653 err = 0;
654 break;
657 case IP_ADD_MEMBERSHIP:
658 case IP_DROP_MEMBERSHIP:
660 struct ip_mreqn mreq;
662 err = -EPROTO;
663 if (inet_sk(sk)->is_icsk)
664 break;
666 if (optlen < sizeof(struct ip_mreq))
667 goto e_inval;
668 err = -EFAULT;
669 if (optlen >= sizeof(struct ip_mreqn)) {
670 if (copy_from_user(&mreq, optval, sizeof(mreq)))
671 break;
672 } else {
673 memset(&mreq, 0, sizeof(mreq));
674 if (copy_from_user(&mreq, optval, sizeof(struct ip_mreq)))
675 break;
678 if (optname == IP_ADD_MEMBERSHIP)
679 err = ip_mc_join_group(sk, &mreq);
680 else
681 err = ip_mc_leave_group(sk, &mreq);
682 break;
684 case IP_MSFILTER:
686 struct ip_msfilter *msf;
688 if (optlen < IP_MSFILTER_SIZE(0))
689 goto e_inval;
690 if (optlen > sysctl_optmem_max) {
691 err = -ENOBUFS;
692 break;
694 msf = kmalloc(optlen, GFP_KERNEL);
695 if (!msf) {
696 err = -ENOBUFS;
697 break;
699 err = -EFAULT;
700 if (copy_from_user(msf, optval, optlen)) {
701 kfree(msf);
702 break;
704 /* numsrc >= (1G-4) overflow in 32 bits */
705 if (msf->imsf_numsrc >= 0x3ffffffcU ||
706 msf->imsf_numsrc > sysctl_igmp_max_msf) {
707 kfree(msf);
708 err = -ENOBUFS;
709 break;
711 if (IP_MSFILTER_SIZE(msf->imsf_numsrc) > optlen) {
712 kfree(msf);
713 err = -EINVAL;
714 break;
716 err = ip_mc_msfilter(sk, msf, 0);
717 kfree(msf);
718 break;
720 case IP_BLOCK_SOURCE:
721 case IP_UNBLOCK_SOURCE:
722 case IP_ADD_SOURCE_MEMBERSHIP:
723 case IP_DROP_SOURCE_MEMBERSHIP:
725 struct ip_mreq_source mreqs;
726 int omode, add;
728 if (optlen != sizeof(struct ip_mreq_source))
729 goto e_inval;
730 if (copy_from_user(&mreqs, optval, sizeof(mreqs))) {
731 err = -EFAULT;
732 break;
734 if (optname == IP_BLOCK_SOURCE) {
735 omode = MCAST_EXCLUDE;
736 add = 1;
737 } else if (optname == IP_UNBLOCK_SOURCE) {
738 omode = MCAST_EXCLUDE;
739 add = 0;
740 } else if (optname == IP_ADD_SOURCE_MEMBERSHIP) {
741 struct ip_mreqn mreq;
743 mreq.imr_multiaddr.s_addr = mreqs.imr_multiaddr;
744 mreq.imr_address.s_addr = mreqs.imr_interface;
745 mreq.imr_ifindex = 0;
746 err = ip_mc_join_group(sk, &mreq);
747 if (err && err != -EADDRINUSE)
748 break;
749 omode = MCAST_INCLUDE;
750 add = 1;
751 } else /* IP_DROP_SOURCE_MEMBERSHIP */ {
752 omode = MCAST_INCLUDE;
753 add = 0;
755 err = ip_mc_source(add, omode, sk, &mreqs, 0);
756 break;
758 case MCAST_JOIN_GROUP:
759 case MCAST_LEAVE_GROUP:
761 struct group_req greq;
762 struct sockaddr_in *psin;
763 struct ip_mreqn mreq;
765 if (optlen < sizeof(struct group_req))
766 goto e_inval;
767 err = -EFAULT;
768 if (copy_from_user(&greq, optval, sizeof(greq)))
769 break;
770 psin = (struct sockaddr_in *)&greq.gr_group;
771 if (psin->sin_family != AF_INET)
772 goto e_inval;
773 memset(&mreq, 0, sizeof(mreq));
774 mreq.imr_multiaddr = psin->sin_addr;
775 mreq.imr_ifindex = greq.gr_interface;
777 if (optname == MCAST_JOIN_GROUP)
778 err = ip_mc_join_group(sk, &mreq);
779 else
780 err = ip_mc_leave_group(sk, &mreq);
781 break;
783 case MCAST_JOIN_SOURCE_GROUP:
784 case MCAST_LEAVE_SOURCE_GROUP:
785 case MCAST_BLOCK_SOURCE:
786 case MCAST_UNBLOCK_SOURCE:
788 struct group_source_req greqs;
789 struct ip_mreq_source mreqs;
790 struct sockaddr_in *psin;
791 int omode, add;
793 if (optlen != sizeof(struct group_source_req))
794 goto e_inval;
795 if (copy_from_user(&greqs, optval, sizeof(greqs))) {
796 err = -EFAULT;
797 break;
799 if (greqs.gsr_group.ss_family != AF_INET ||
800 greqs.gsr_source.ss_family != AF_INET) {
801 err = -EADDRNOTAVAIL;
802 break;
804 psin = (struct sockaddr_in *)&greqs.gsr_group;
805 mreqs.imr_multiaddr = psin->sin_addr.s_addr;
806 psin = (struct sockaddr_in *)&greqs.gsr_source;
807 mreqs.imr_sourceaddr = psin->sin_addr.s_addr;
808 mreqs.imr_interface = 0; /* use index for mc_source */
810 if (optname == MCAST_BLOCK_SOURCE) {
811 omode = MCAST_EXCLUDE;
812 add = 1;
813 } else if (optname == MCAST_UNBLOCK_SOURCE) {
814 omode = MCAST_EXCLUDE;
815 add = 0;
816 } else if (optname == MCAST_JOIN_SOURCE_GROUP) {
817 struct ip_mreqn mreq;
819 psin = (struct sockaddr_in *)&greqs.gsr_group;
820 mreq.imr_multiaddr = psin->sin_addr;
821 mreq.imr_address.s_addr = 0;
822 mreq.imr_ifindex = greqs.gsr_interface;
823 err = ip_mc_join_group(sk, &mreq);
824 if (err && err != -EADDRINUSE)
825 break;
826 greqs.gsr_interface = mreq.imr_ifindex;
827 omode = MCAST_INCLUDE;
828 add = 1;
829 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
830 omode = MCAST_INCLUDE;
831 add = 0;
833 err = ip_mc_source(add, omode, sk, &mreqs,
834 greqs.gsr_interface);
835 break;
837 case MCAST_MSFILTER:
839 struct sockaddr_in *psin;
840 struct ip_msfilter *msf = NULL;
841 struct group_filter *gsf = NULL;
842 int msize, i, ifindex;
844 if (optlen < GROUP_FILTER_SIZE(0))
845 goto e_inval;
846 if (optlen > sysctl_optmem_max) {
847 err = -ENOBUFS;
848 break;
850 gsf = kmalloc(optlen, GFP_KERNEL);
851 if (!gsf) {
852 err = -ENOBUFS;
853 break;
855 err = -EFAULT;
856 if (copy_from_user(gsf, optval, optlen))
857 goto mc_msf_out;
859 /* numsrc >= (4G-140)/128 overflow in 32 bits */
860 if (gsf->gf_numsrc >= 0x1ffffff ||
861 gsf->gf_numsrc > sysctl_igmp_max_msf) {
862 err = -ENOBUFS;
863 goto mc_msf_out;
865 if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) {
866 err = -EINVAL;
867 goto mc_msf_out;
869 msize = IP_MSFILTER_SIZE(gsf->gf_numsrc);
870 msf = kmalloc(msize, GFP_KERNEL);
871 if (!msf) {
872 err = -ENOBUFS;
873 goto mc_msf_out;
875 ifindex = gsf->gf_interface;
876 psin = (struct sockaddr_in *)&gsf->gf_group;
877 if (psin->sin_family != AF_INET) {
878 err = -EADDRNOTAVAIL;
879 goto mc_msf_out;
881 msf->imsf_multiaddr = psin->sin_addr.s_addr;
882 msf->imsf_interface = 0;
883 msf->imsf_fmode = gsf->gf_fmode;
884 msf->imsf_numsrc = gsf->gf_numsrc;
885 err = -EADDRNOTAVAIL;
886 for (i = 0; i < gsf->gf_numsrc; ++i) {
887 psin = (struct sockaddr_in *)&gsf->gf_slist[i];
889 if (psin->sin_family != AF_INET)
890 goto mc_msf_out;
891 msf->imsf_slist[i] = psin->sin_addr.s_addr;
893 kfree(gsf);
894 gsf = NULL;
896 err = ip_mc_msfilter(sk, msf, ifindex);
897 mc_msf_out:
898 kfree(msf);
899 kfree(gsf);
900 break;
902 case IP_MULTICAST_ALL:
903 if (optlen < 1)
904 goto e_inval;
905 if (val != 0 && val != 1)
906 goto e_inval;
907 inet->mc_all = val;
908 break;
909 case IP_ROUTER_ALERT:
910 err = ip_ra_control(sk, val ? 1 : 0, NULL);
911 break;
913 case IP_FREEBIND:
914 if (optlen < 1)
915 goto e_inval;
916 inet->freebind = !!val;
917 break;
919 case IP_IPSEC_POLICY:
920 case IP_XFRM_POLICY:
921 err = -EPERM;
922 if (!capable(CAP_NET_ADMIN))
923 break;
924 err = xfrm_user_policy(sk, optname, optval, optlen);
925 break;
927 case IP_TRANSPARENT:
928 if (!capable(CAP_NET_ADMIN)) {
929 err = -EPERM;
930 break;
932 if (optlen < 1)
933 goto e_inval;
934 inet->transparent = !!val;
935 break;
937 default:
938 err = -ENOPROTOOPT;
939 break;
941 release_sock(sk);
942 return err;
944 e_inval:
945 release_sock(sk);
946 return -EINVAL;
949 int ip_setsockopt(struct sock *sk, int level,
950 int optname, char __user *optval, int optlen)
952 int err;
954 if (level != SOL_IP)
955 return -ENOPROTOOPT;
957 err = do_ip_setsockopt(sk, level, optname, optval, optlen);
958 #ifdef CONFIG_NETFILTER
959 /* we need to exclude all possible ENOPROTOOPTs except default case */
960 if (err == -ENOPROTOOPT && optname != IP_HDRINCL &&
961 optname != IP_IPSEC_POLICY &&
962 optname != IP_XFRM_POLICY &&
963 !ip_mroute_opt(optname)) {
964 lock_sock(sk);
965 err = nf_setsockopt(sk, PF_INET, optname, optval, optlen);
966 release_sock(sk);
968 #endif
969 return err;
971 EXPORT_SYMBOL(ip_setsockopt);
973 #ifdef CONFIG_COMPAT
974 int compat_ip_setsockopt(struct sock *sk, int level, int optname,
975 char __user *optval, int optlen)
977 int err;
979 if (level != SOL_IP)
980 return -ENOPROTOOPT;
982 if (optname >= MCAST_JOIN_GROUP && optname <= MCAST_MSFILTER)
983 return compat_mc_setsockopt(sk, level, optname, optval, optlen,
984 ip_setsockopt);
986 err = do_ip_setsockopt(sk, level, optname, optval, optlen);
987 #ifdef CONFIG_NETFILTER
988 /* we need to exclude all possible ENOPROTOOPTs except default case */
989 if (err == -ENOPROTOOPT && optname != IP_HDRINCL &&
990 optname != IP_IPSEC_POLICY &&
991 optname != IP_XFRM_POLICY &&
992 !ip_mroute_opt(optname)) {
993 lock_sock(sk);
994 err = compat_nf_setsockopt(sk, PF_INET, optname,
995 optval, optlen);
996 release_sock(sk);
998 #endif
999 return err;
1001 EXPORT_SYMBOL(compat_ip_setsockopt);
1002 #endif
1005 * Get the options. Note for future reference. The GET of IP options gets
1006 * the _received_ ones. The set sets the _sent_ ones.
1009 static int do_ip_getsockopt(struct sock *sk, int level, int optname,
1010 char __user *optval, int __user *optlen)
1012 struct inet_sock *inet = inet_sk(sk);
1013 int val;
1014 int len;
1016 if (level != SOL_IP)
1017 return -EOPNOTSUPP;
1019 if (ip_mroute_opt(optname))
1020 return ip_mroute_getsockopt(sk, optname, optval, optlen);
1022 if (get_user(len, optlen))
1023 return -EFAULT;
1024 if (len < 0)
1025 return -EINVAL;
1027 lock_sock(sk);
1029 switch (optname) {
1030 case IP_OPTIONS:
1032 unsigned char optbuf[sizeof(struct ip_options)+40];
1033 struct ip_options * opt = (struct ip_options *)optbuf;
1034 opt->optlen = 0;
1035 if (inet->opt)
1036 memcpy(optbuf, inet->opt,
1037 sizeof(struct ip_options)+
1038 inet->opt->optlen);
1039 release_sock(sk);
1041 if (opt->optlen == 0)
1042 return put_user(0, optlen);
1044 ip_options_undo(opt);
1046 len = min_t(unsigned int, len, opt->optlen);
1047 if (put_user(len, optlen))
1048 return -EFAULT;
1049 if (copy_to_user(optval, opt->__data, len))
1050 return -EFAULT;
1051 return 0;
1053 case IP_PKTINFO:
1054 val = (inet->cmsg_flags & IP_CMSG_PKTINFO) != 0;
1055 break;
1056 case IP_RECVTTL:
1057 val = (inet->cmsg_flags & IP_CMSG_TTL) != 0;
1058 break;
1059 case IP_RECVTOS:
1060 val = (inet->cmsg_flags & IP_CMSG_TOS) != 0;
1061 break;
1062 case IP_RECVOPTS:
1063 val = (inet->cmsg_flags & IP_CMSG_RECVOPTS) != 0;
1064 break;
1065 case IP_RETOPTS:
1066 val = (inet->cmsg_flags & IP_CMSG_RETOPTS) != 0;
1067 break;
1068 case IP_PASSSEC:
1069 val = (inet->cmsg_flags & IP_CMSG_PASSSEC) != 0;
1070 break;
1071 case IP_RECVORIGDSTADDR:
1072 val = (inet->cmsg_flags & IP_CMSG_ORIGDSTADDR) != 0;
1073 break;
1074 case IP_TOS:
1075 val = inet->tos;
1076 break;
1077 case IP_TTL:
1078 val = (inet->uc_ttl == -1 ?
1079 sysctl_ip_default_ttl :
1080 inet->uc_ttl);
1081 break;
1082 case IP_HDRINCL:
1083 val = inet->hdrincl;
1084 break;
1085 case IP_MTU_DISCOVER:
1086 val = inet->pmtudisc;
1087 break;
1088 case IP_MTU:
1090 struct dst_entry *dst;
1091 val = 0;
1092 dst = sk_dst_get(sk);
1093 if (dst) {
1094 val = dst_mtu(dst);
1095 dst_release(dst);
1097 if (!val) {
1098 release_sock(sk);
1099 return -ENOTCONN;
1101 break;
1103 case IP_RECVERR:
1104 val = inet->recverr;
1105 break;
1106 case IP_MULTICAST_TTL:
1107 val = inet->mc_ttl;
1108 break;
1109 case IP_MULTICAST_LOOP:
1110 val = inet->mc_loop;
1111 break;
1112 case IP_MULTICAST_IF:
1114 struct in_addr addr;
1115 len = min_t(unsigned int, len, sizeof(struct in_addr));
1116 addr.s_addr = inet->mc_addr;
1117 release_sock(sk);
1119 if (put_user(len, optlen))
1120 return -EFAULT;
1121 if (copy_to_user(optval, &addr, len))
1122 return -EFAULT;
1123 return 0;
1125 case IP_MSFILTER:
1127 struct ip_msfilter msf;
1128 int err;
1130 if (len < IP_MSFILTER_SIZE(0)) {
1131 release_sock(sk);
1132 return -EINVAL;
1134 if (copy_from_user(&msf, optval, IP_MSFILTER_SIZE(0))) {
1135 release_sock(sk);
1136 return -EFAULT;
1138 err = ip_mc_msfget(sk, &msf,
1139 (struct ip_msfilter __user *)optval, optlen);
1140 release_sock(sk);
1141 return err;
1143 case MCAST_MSFILTER:
1145 struct group_filter gsf;
1146 int err;
1148 if (len < GROUP_FILTER_SIZE(0)) {
1149 release_sock(sk);
1150 return -EINVAL;
1152 if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0))) {
1153 release_sock(sk);
1154 return -EFAULT;
1156 err = ip_mc_gsfget(sk, &gsf,
1157 (struct group_filter __user *)optval,
1158 optlen);
1159 release_sock(sk);
1160 return err;
1162 case IP_MULTICAST_ALL:
1163 val = inet->mc_all;
1164 break;
1165 case IP_PKTOPTIONS:
1167 struct msghdr msg;
1169 release_sock(sk);
1171 if (sk->sk_type != SOCK_STREAM)
1172 return -ENOPROTOOPT;
1174 msg.msg_control = optval;
1175 msg.msg_controllen = len;
1176 msg.msg_flags = 0;
1178 if (inet->cmsg_flags & IP_CMSG_PKTINFO) {
1179 struct in_pktinfo info;
1181 info.ipi_addr.s_addr = inet->rcv_saddr;
1182 info.ipi_spec_dst.s_addr = inet->rcv_saddr;
1183 info.ipi_ifindex = inet->mc_index;
1184 put_cmsg(&msg, SOL_IP, IP_PKTINFO, sizeof(info), &info);
1186 if (inet->cmsg_flags & IP_CMSG_TTL) {
1187 int hlim = inet->mc_ttl;
1188 put_cmsg(&msg, SOL_IP, IP_TTL, sizeof(hlim), &hlim);
1190 len -= msg.msg_controllen;
1191 return put_user(len, optlen);
1193 case IP_FREEBIND:
1194 val = inet->freebind;
1195 break;
1196 case IP_TRANSPARENT:
1197 val = inet->transparent;
1198 break;
1199 default:
1200 release_sock(sk);
1201 return -ENOPROTOOPT;
1203 release_sock(sk);
1205 if (len < sizeof(int) && len > 0 && val >= 0 && val <= 255) {
1206 unsigned char ucval = (unsigned char)val;
1207 len = 1;
1208 if (put_user(len, optlen))
1209 return -EFAULT;
1210 if (copy_to_user(optval, &ucval, 1))
1211 return -EFAULT;
1212 } else {
1213 len = min_t(unsigned int, sizeof(int), len);
1214 if (put_user(len, optlen))
1215 return -EFAULT;
1216 if (copy_to_user(optval, &val, len))
1217 return -EFAULT;
1219 return 0;
1222 int ip_getsockopt(struct sock *sk, int level,
1223 int optname, char __user *optval, int __user *optlen)
1225 int err;
1227 err = do_ip_getsockopt(sk, level, optname, optval, optlen);
1228 #ifdef CONFIG_NETFILTER
1229 /* we need to exclude all possible ENOPROTOOPTs except default case */
1230 if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS &&
1231 !ip_mroute_opt(optname)) {
1232 int len;
1234 if (get_user(len, optlen))
1235 return -EFAULT;
1237 lock_sock(sk);
1238 err = nf_getsockopt(sk, PF_INET, optname, optval,
1239 &len);
1240 release_sock(sk);
1241 if (err >= 0)
1242 err = put_user(len, optlen);
1243 return err;
1245 #endif
1246 return err;
1248 EXPORT_SYMBOL(ip_getsockopt);
1250 #ifdef CONFIG_COMPAT
1251 int compat_ip_getsockopt(struct sock *sk, int level, int optname,
1252 char __user *optval, int __user *optlen)
1254 int err;
1256 if (optname == MCAST_MSFILTER)
1257 return compat_mc_getsockopt(sk, level, optname, optval, optlen,
1258 ip_getsockopt);
1260 err = do_ip_getsockopt(sk, level, optname, optval, optlen);
1262 #ifdef CONFIG_NETFILTER
1263 /* we need to exclude all possible ENOPROTOOPTs except default case */
1264 if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS &&
1265 !ip_mroute_opt(optname)) {
1266 int len;
1268 if (get_user(len, optlen))
1269 return -EFAULT;
1271 lock_sock(sk);
1272 err = compat_nf_getsockopt(sk, PF_INET, optname, optval, &len);
1273 release_sock(sk);
1274 if (err >= 0)
1275 err = put_user(len, optlen);
1276 return err;
1278 #endif
1279 return err;
1281 EXPORT_SYMBOL(compat_ip_getsockopt);
1282 #endif