blob: 2f0b47da5b37e3b3c0c44235355e704958cba8a3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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.
5 *
6 * The IP to API glue.
7 *
8 * Version: $Id: ip_sockglue.c,v 1.62 2002/02/01 22:01:04 davem Exp $
9 *
10 * Authors: see ip.c
11 *
12 * Fixes:
13 * Many : Split from ip.c , see ip.c for history.
14 * Martin Mares : TOS setting fixed.
15 * Alan Cox : Fixed a couple of oopses in Martin's
16 * TOS tweaks.
17 * Mike McLagan : Routing by source
18 */
19
20#include <linux/config.h>
21#include <linux/module.h>
22#include <linux/types.h>
23#include <linux/mm.h>
24#include <linux/sched.h>
25#include <linux/skbuff.h>
26#include <linux/ip.h>
27#include <linux/icmp.h>
28#include <linux/netdevice.h>
29#include <net/sock.h>
30#include <net/ip.h>
31#include <net/icmp.h>
32#include <net/tcp.h>
33#include <linux/tcp.h>
34#include <linux/udp.h>
35#include <linux/igmp.h>
36#include <linux/netfilter.h>
37#include <linux/route.h>
38#include <linux/mroute.h>
39#include <net/route.h>
40#include <net/xfrm.h>
41#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
42#include <net/transp_v6.h>
43#endif
44
45#include <linux/errqueue.h>
46#include <asm/uaccess.h>
47
48#define IP_CMSG_PKTINFO 1
49#define IP_CMSG_TTL 2
50#define IP_CMSG_TOS 4
51#define IP_CMSG_RECVOPTS 8
52#define IP_CMSG_RETOPTS 16
53
54/*
55 * SOL_IP control messages.
56 */
57
58static void ip_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb)
59{
60 struct in_pktinfo info;
61 struct rtable *rt = (struct rtable *)skb->dst;
62
63 info.ipi_addr.s_addr = skb->nh.iph->daddr;
64 if (rt) {
65 info.ipi_ifindex = rt->rt_iif;
66 info.ipi_spec_dst.s_addr = rt->rt_spec_dst;
67 } else {
68 info.ipi_ifindex = 0;
69 info.ipi_spec_dst.s_addr = 0;
70 }
71
72 put_cmsg(msg, SOL_IP, IP_PKTINFO, sizeof(info), &info);
73}
74
75static void ip_cmsg_recv_ttl(struct msghdr *msg, struct sk_buff *skb)
76{
77 int ttl = skb->nh.iph->ttl;
78 put_cmsg(msg, SOL_IP, IP_TTL, sizeof(int), &ttl);
79}
80
81static void ip_cmsg_recv_tos(struct msghdr *msg, struct sk_buff *skb)
82{
83 put_cmsg(msg, SOL_IP, IP_TOS, 1, &skb->nh.iph->tos);
84}
85
86static void ip_cmsg_recv_opts(struct msghdr *msg, struct sk_buff *skb)
87{
88 if (IPCB(skb)->opt.optlen == 0)
89 return;
90
91 put_cmsg(msg, SOL_IP, IP_RECVOPTS, IPCB(skb)->opt.optlen, skb->nh.iph+1);
92}
93
94
95static void ip_cmsg_recv_retopts(struct msghdr *msg, struct sk_buff *skb)
96{
97 unsigned char optbuf[sizeof(struct ip_options) + 40];
98 struct ip_options * opt = (struct ip_options*)optbuf;
99
100 if (IPCB(skb)->opt.optlen == 0)
101 return;
102
103 if (ip_options_echo(opt, skb)) {
104 msg->msg_flags |= MSG_CTRUNC;
105 return;
106 }
107 ip_options_undo(opt);
108
109 put_cmsg(msg, SOL_IP, IP_RETOPTS, opt->optlen, opt->__data);
110}
111
112
113void ip_cmsg_recv(struct msghdr *msg, struct sk_buff *skb)
114{
115 struct inet_sock *inet = inet_sk(skb->sk);
116 unsigned flags = inet->cmsg_flags;
117
118 /* Ordered by supposed usage frequency */
119 if (flags & 1)
120 ip_cmsg_recv_pktinfo(msg, skb);
121 if ((flags>>=1) == 0)
122 return;
123
124 if (flags & 1)
125 ip_cmsg_recv_ttl(msg, skb);
126 if ((flags>>=1) == 0)
127 return;
128
129 if (flags & 1)
130 ip_cmsg_recv_tos(msg, skb);
131 if ((flags>>=1) == 0)
132 return;
133
134 if (flags & 1)
135 ip_cmsg_recv_opts(msg, skb);
136 if ((flags>>=1) == 0)
137 return;
138
139 if (flags & 1)
140 ip_cmsg_recv_retopts(msg, skb);
141}
142
143int ip_cmsg_send(struct msghdr *msg, struct ipcm_cookie *ipc)
144{
145 int err;
146 struct cmsghdr *cmsg;
147
148 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
149 if (!CMSG_OK(msg, cmsg))
150 return -EINVAL;
151 if (cmsg->cmsg_level != SOL_IP)
152 continue;
153 switch (cmsg->cmsg_type) {
154 case IP_RETOPTS:
155 err = cmsg->cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr));
Arnaldo Carvalho de Melo4c6ea292005-08-16 19:46:48 -0300156 err = ip_options_get(&ipc->opt, CMSG_DATA(cmsg), err < 40 ? err : 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 if (err)
158 return err;
159 break;
160 case IP_PKTINFO:
161 {
162 struct in_pktinfo *info;
163 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct in_pktinfo)))
164 return -EINVAL;
165 info = (struct in_pktinfo *)CMSG_DATA(cmsg);
166 ipc->oif = info->ipi_ifindex;
167 ipc->addr = info->ipi_spec_dst.s_addr;
168 break;
169 }
170 default:
171 return -EINVAL;
172 }
173 }
174 return 0;
175}
176
177
178/* Special input handler for packets caught by router alert option.
179 They are selected only by protocol field, and then processed likely
180 local ones; but only if someone wants them! Otherwise, router
181 not running rsvpd will kill RSVP.
182
183 It is user level problem, what it will make with them.
184 I have no idea, how it will masquearde or NAT them (it is joke, joke :-)),
185 but receiver should be enough clever f.e. to forward mtrace requests,
186 sent to multicast group to reach destination designated router.
187 */
188struct ip_ra_chain *ip_ra_chain;
189DEFINE_RWLOCK(ip_ra_lock);
190
191int ip_ra_control(struct sock *sk, unsigned char on, void (*destructor)(struct sock *))
192{
193 struct ip_ra_chain *ra, *new_ra, **rap;
194
195 if (sk->sk_type != SOCK_RAW || inet_sk(sk)->num == IPPROTO_RAW)
196 return -EINVAL;
197
198 new_ra = on ? kmalloc(sizeof(*new_ra), GFP_KERNEL) : NULL;
199
200 write_lock_bh(&ip_ra_lock);
201 for (rap = &ip_ra_chain; (ra=*rap) != NULL; rap = &ra->next) {
202 if (ra->sk == sk) {
203 if (on) {
204 write_unlock_bh(&ip_ra_lock);
205 if (new_ra)
206 kfree(new_ra);
207 return -EADDRINUSE;
208 }
209 *rap = ra->next;
210 write_unlock_bh(&ip_ra_lock);
211
212 if (ra->destructor)
213 ra->destructor(sk);
214 sock_put(sk);
215 kfree(ra);
216 return 0;
217 }
218 }
219 if (new_ra == NULL) {
220 write_unlock_bh(&ip_ra_lock);
221 return -ENOBUFS;
222 }
223 new_ra->sk = sk;
224 new_ra->destructor = destructor;
225
226 new_ra->next = ra;
227 *rap = new_ra;
228 sock_hold(sk);
229 write_unlock_bh(&ip_ra_lock);
230
231 return 0;
232}
233
234void ip_icmp_error(struct sock *sk, struct sk_buff *skb, int err,
235 u16 port, u32 info, u8 *payload)
236{
237 struct inet_sock *inet = inet_sk(sk);
238 struct sock_exterr_skb *serr;
239
240 if (!inet->recverr)
241 return;
242
243 skb = skb_clone(skb, GFP_ATOMIC);
244 if (!skb)
245 return;
246
247 serr = SKB_EXT_ERR(skb);
248 serr->ee.ee_errno = err;
249 serr->ee.ee_origin = SO_EE_ORIGIN_ICMP;
250 serr->ee.ee_type = skb->h.icmph->type;
251 serr->ee.ee_code = skb->h.icmph->code;
252 serr->ee.ee_pad = 0;
253 serr->ee.ee_info = info;
254 serr->ee.ee_data = 0;
255 serr->addr_offset = (u8*)&(((struct iphdr*)(skb->h.icmph+1))->daddr) - skb->nh.raw;
256 serr->port = port;
257
258 skb->h.raw = payload;
259 if (!skb_pull(skb, payload - skb->data) ||
260 sock_queue_err_skb(sk, skb))
261 kfree_skb(skb);
262}
263
264void ip_local_error(struct sock *sk, int err, u32 daddr, u16 port, u32 info)
265{
266 struct inet_sock *inet = inet_sk(sk);
267 struct sock_exterr_skb *serr;
268 struct iphdr *iph;
269 struct sk_buff *skb;
270
271 if (!inet->recverr)
272 return;
273
274 skb = alloc_skb(sizeof(struct iphdr), GFP_ATOMIC);
275 if (!skb)
276 return;
277
278 iph = (struct iphdr*)skb_put(skb, sizeof(struct iphdr));
279 skb->nh.iph = iph;
280 iph->daddr = daddr;
281
282 serr = SKB_EXT_ERR(skb);
283 serr->ee.ee_errno = err;
284 serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL;
285 serr->ee.ee_type = 0;
286 serr->ee.ee_code = 0;
287 serr->ee.ee_pad = 0;
288 serr->ee.ee_info = info;
289 serr->ee.ee_data = 0;
290 serr->addr_offset = (u8*)&iph->daddr - skb->nh.raw;
291 serr->port = port;
292
293 skb->h.raw = skb->tail;
294 __skb_pull(skb, skb->tail - skb->data);
295
296 if (sock_queue_err_skb(sk, skb))
297 kfree_skb(skb);
298}
299
300/*
301 * Handle MSG_ERRQUEUE
302 */
303int ip_recv_error(struct sock *sk, struct msghdr *msg, int len)
304{
305 struct sock_exterr_skb *serr;
306 struct sk_buff *skb, *skb2;
307 struct sockaddr_in *sin;
308 struct {
309 struct sock_extended_err ee;
310 struct sockaddr_in offender;
311 } errhdr;
312 int err;
313 int copied;
314
315 err = -EAGAIN;
316 skb = skb_dequeue(&sk->sk_error_queue);
317 if (skb == NULL)
318 goto out;
319
320 copied = skb->len;
321 if (copied > len) {
322 msg->msg_flags |= MSG_TRUNC;
323 copied = len;
324 }
325 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
326 if (err)
327 goto out_free_skb;
328
329 sock_recv_timestamp(msg, sk, skb);
330
331 serr = SKB_EXT_ERR(skb);
332
333 sin = (struct sockaddr_in *)msg->msg_name;
334 if (sin) {
335 sin->sin_family = AF_INET;
336 sin->sin_addr.s_addr = *(u32*)(skb->nh.raw + serr->addr_offset);
337 sin->sin_port = serr->port;
338 memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
339 }
340
341 memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err));
342 sin = &errhdr.offender;
343 sin->sin_family = AF_UNSPEC;
344 if (serr->ee.ee_origin == SO_EE_ORIGIN_ICMP) {
345 struct inet_sock *inet = inet_sk(sk);
346
347 sin->sin_family = AF_INET;
348 sin->sin_addr.s_addr = skb->nh.iph->saddr;
349 sin->sin_port = 0;
350 memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
351 if (inet->cmsg_flags)
352 ip_cmsg_recv(msg, skb);
353 }
354
355 put_cmsg(msg, SOL_IP, IP_RECVERR, sizeof(errhdr), &errhdr);
356
357 /* Now we could try to dump offended packet options */
358
359 msg->msg_flags |= MSG_ERRQUEUE;
360 err = copied;
361
362 /* Reset and regenerate socket error */
Herbert Xue0f9f852005-06-18 22:56:18 -0700363 spin_lock_bh(&sk->sk_error_queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 sk->sk_err = 0;
365 if ((skb2 = skb_peek(&sk->sk_error_queue)) != NULL) {
366 sk->sk_err = SKB_EXT_ERR(skb2)->ee.ee_errno;
Herbert Xue0f9f852005-06-18 22:56:18 -0700367 spin_unlock_bh(&sk->sk_error_queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 sk->sk_error_report(sk);
369 } else
Herbert Xue0f9f852005-06-18 22:56:18 -0700370 spin_unlock_bh(&sk->sk_error_queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
372out_free_skb:
373 kfree_skb(skb);
374out:
375 return err;
376}
377
378
379/*
380 * Socket option code for IP. This is the end of the line after any TCP,UDP etc options on
381 * an IP socket.
382 */
383
384int ip_setsockopt(struct sock *sk, int level, int optname, char __user *optval, int optlen)
385{
386 struct inet_sock *inet = inet_sk(sk);
387 int val=0,err;
388
389 if (level != SOL_IP)
390 return -ENOPROTOOPT;
391
392 if (((1<<optname) & ((1<<IP_PKTINFO) | (1<<IP_RECVTTL) |
393 (1<<IP_RECVOPTS) | (1<<IP_RECVTOS) |
394 (1<<IP_RETOPTS) | (1<<IP_TOS) |
395 (1<<IP_TTL) | (1<<IP_HDRINCL) |
396 (1<<IP_MTU_DISCOVER) | (1<<IP_RECVERR) |
397 (1<<IP_ROUTER_ALERT) | (1<<IP_FREEBIND))) ||
398 optname == IP_MULTICAST_TTL ||
399 optname == IP_MULTICAST_LOOP) {
400 if (optlen >= sizeof(int)) {
401 if (get_user(val, (int __user *) optval))
402 return -EFAULT;
403 } else if (optlen >= sizeof(char)) {
404 unsigned char ucval;
405
406 if (get_user(ucval, (unsigned char __user *) optval))
407 return -EFAULT;
408 val = (int) ucval;
409 }
410 }
411
412 /* If optlen==0, it is equivalent to val == 0 */
413
414#ifdef CONFIG_IP_MROUTE
415 if (optname >= MRT_BASE && optname <= (MRT_BASE + 10))
416 return ip_mroute_setsockopt(sk,optname,optval,optlen);
417#endif
418
419 err = 0;
420 lock_sock(sk);
421
422 switch (optname) {
423 case IP_OPTIONS:
424 {
425 struct ip_options * opt = NULL;
426 if (optlen > 40 || optlen < 0)
427 goto e_inval;
Arnaldo Carvalho de Melo4c6ea292005-08-16 19:46:48 -0300428 err = ip_options_get_from_user(&opt, optval, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 if (err)
430 break;
431 if (sk->sk_type == SOCK_STREAM) {
432 struct tcp_sock *tp = tcp_sk(sk);
433#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
434 if (sk->sk_family == PF_INET ||
435 (!((1 << sk->sk_state) &
436 (TCPF_LISTEN | TCPF_CLOSE)) &&
437 inet->daddr != LOOPBACK4_IPV6)) {
438#endif
439 if (inet->opt)
440 tp->ext_header_len -= inet->opt->optlen;
441 if (opt)
442 tp->ext_header_len += opt->optlen;
443 tcp_sync_mss(sk, tp->pmtu_cookie);
444#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
445 }
446#endif
447 }
448 opt = xchg(&inet->opt, opt);
449 if (opt)
450 kfree(opt);
451 break;
452 }
453 case IP_PKTINFO:
454 if (val)
455 inet->cmsg_flags |= IP_CMSG_PKTINFO;
456 else
457 inet->cmsg_flags &= ~IP_CMSG_PKTINFO;
458 break;
459 case IP_RECVTTL:
460 if (val)
461 inet->cmsg_flags |= IP_CMSG_TTL;
462 else
463 inet->cmsg_flags &= ~IP_CMSG_TTL;
464 break;
465 case IP_RECVTOS:
466 if (val)
467 inet->cmsg_flags |= IP_CMSG_TOS;
468 else
469 inet->cmsg_flags &= ~IP_CMSG_TOS;
470 break;
471 case IP_RECVOPTS:
472 if (val)
473 inet->cmsg_flags |= IP_CMSG_RECVOPTS;
474 else
475 inet->cmsg_flags &= ~IP_CMSG_RECVOPTS;
476 break;
477 case IP_RETOPTS:
478 if (val)
479 inet->cmsg_flags |= IP_CMSG_RETOPTS;
480 else
481 inet->cmsg_flags &= ~IP_CMSG_RETOPTS;
482 break;
483 case IP_TOS: /* This sets both TOS and Precedence */
484 if (sk->sk_type == SOCK_STREAM) {
485 val &= ~3;
486 val |= inet->tos & 3;
487 }
488 if (IPTOS_PREC(val) >= IPTOS_PREC_CRITIC_ECP &&
489 !capable(CAP_NET_ADMIN)) {
490 err = -EPERM;
491 break;
492 }
493 if (inet->tos != val) {
494 inet->tos = val;
495 sk->sk_priority = rt_tos2priority(val);
496 sk_dst_reset(sk);
497 }
498 break;
499 case IP_TTL:
500 if (optlen<1)
501 goto e_inval;
502 if (val != -1 && (val < 1 || val>255))
503 goto e_inval;
504 inet->uc_ttl = val;
505 break;
506 case IP_HDRINCL:
507 if (sk->sk_type != SOCK_RAW) {
508 err = -ENOPROTOOPT;
509 break;
510 }
511 inet->hdrincl = val ? 1 : 0;
512 break;
513 case IP_MTU_DISCOVER:
514 if (val<0 || val>2)
515 goto e_inval;
516 inet->pmtudisc = val;
517 break;
518 case IP_RECVERR:
519 inet->recverr = !!val;
520 if (!val)
521 skb_queue_purge(&sk->sk_error_queue);
522 break;
523 case IP_MULTICAST_TTL:
524 if (sk->sk_type == SOCK_STREAM)
525 goto e_inval;
526 if (optlen<1)
527 goto e_inval;
528 if (val==-1)
529 val = 1;
530 if (val < 0 || val > 255)
531 goto e_inval;
532 inet->mc_ttl = val;
533 break;
534 case IP_MULTICAST_LOOP:
535 if (optlen<1)
536 goto e_inval;
537 inet->mc_loop = !!val;
538 break;
539 case IP_MULTICAST_IF:
540 {
541 struct ip_mreqn mreq;
542 struct net_device *dev = NULL;
543
544 if (sk->sk_type == SOCK_STREAM)
545 goto e_inval;
546 /*
547 * Check the arguments are allowable
548 */
549
550 err = -EFAULT;
551 if (optlen >= sizeof(struct ip_mreqn)) {
552 if (copy_from_user(&mreq,optval,sizeof(mreq)))
553 break;
554 } else {
555 memset(&mreq, 0, sizeof(mreq));
556 if (optlen >= sizeof(struct in_addr) &&
557 copy_from_user(&mreq.imr_address,optval,sizeof(struct in_addr)))
558 break;
559 }
560
561 if (!mreq.imr_ifindex) {
562 if (mreq.imr_address.s_addr == INADDR_ANY) {
563 inet->mc_index = 0;
564 inet->mc_addr = 0;
565 err = 0;
566 break;
567 }
568 dev = ip_dev_find(mreq.imr_address.s_addr);
569 if (dev) {
570 mreq.imr_ifindex = dev->ifindex;
571 dev_put(dev);
572 }
573 } else
574 dev = __dev_get_by_index(mreq.imr_ifindex);
575
576
577 err = -EADDRNOTAVAIL;
578 if (!dev)
579 break;
580
581 err = -EINVAL;
582 if (sk->sk_bound_dev_if &&
583 mreq.imr_ifindex != sk->sk_bound_dev_if)
584 break;
585
586 inet->mc_index = mreq.imr_ifindex;
587 inet->mc_addr = mreq.imr_address.s_addr;
588 err = 0;
589 break;
590 }
591
592 case IP_ADD_MEMBERSHIP:
593 case IP_DROP_MEMBERSHIP:
594 {
595 struct ip_mreqn mreq;
596
597 if (optlen < sizeof(struct ip_mreq))
598 goto e_inval;
599 err = -EFAULT;
600 if (optlen >= sizeof(struct ip_mreqn)) {
601 if(copy_from_user(&mreq,optval,sizeof(mreq)))
602 break;
603 } else {
604 memset(&mreq, 0, sizeof(mreq));
605 if (copy_from_user(&mreq,optval,sizeof(struct ip_mreq)))
606 break;
607 }
608
609 if (optname == IP_ADD_MEMBERSHIP)
610 err = ip_mc_join_group(sk, &mreq);
611 else
612 err = ip_mc_leave_group(sk, &mreq);
613 break;
614 }
615 case IP_MSFILTER:
616 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 extern int sysctl_igmp_max_msf;
618 struct ip_msfilter *msf;
619
620 if (optlen < IP_MSFILTER_SIZE(0))
621 goto e_inval;
622 if (optlen > sysctl_optmem_max) {
623 err = -ENOBUFS;
624 break;
625 }
626 msf = (struct ip_msfilter *)kmalloc(optlen, GFP_KERNEL);
627 if (msf == 0) {
628 err = -ENOBUFS;
629 break;
630 }
631 err = -EFAULT;
632 if (copy_from_user(msf, optval, optlen)) {
633 kfree(msf);
634 break;
635 }
636 /* numsrc >= (1G-4) overflow in 32 bits */
637 if (msf->imsf_numsrc >= 0x3ffffffcU ||
638 msf->imsf_numsrc > sysctl_igmp_max_msf) {
639 kfree(msf);
640 err = -ENOBUFS;
641 break;
642 }
643 if (IP_MSFILTER_SIZE(msf->imsf_numsrc) > optlen) {
644 kfree(msf);
645 err = -EINVAL;
646 break;
647 }
648 err = ip_mc_msfilter(sk, msf, 0);
649 kfree(msf);
650 break;
651 }
652 case IP_BLOCK_SOURCE:
653 case IP_UNBLOCK_SOURCE:
654 case IP_ADD_SOURCE_MEMBERSHIP:
655 case IP_DROP_SOURCE_MEMBERSHIP:
656 {
657 struct ip_mreq_source mreqs;
658 int omode, add;
659
660 if (optlen != sizeof(struct ip_mreq_source))
661 goto e_inval;
662 if (copy_from_user(&mreqs, optval, sizeof(mreqs))) {
663 err = -EFAULT;
664 break;
665 }
666 if (optname == IP_BLOCK_SOURCE) {
667 omode = MCAST_EXCLUDE;
668 add = 1;
669 } else if (optname == IP_UNBLOCK_SOURCE) {
670 omode = MCAST_EXCLUDE;
671 add = 0;
672 } else if (optname == IP_ADD_SOURCE_MEMBERSHIP) {
673 struct ip_mreqn mreq;
674
675 mreq.imr_multiaddr.s_addr = mreqs.imr_multiaddr;
676 mreq.imr_address.s_addr = mreqs.imr_interface;
677 mreq.imr_ifindex = 0;
678 err = ip_mc_join_group(sk, &mreq);
David L Stevens8cdaaa12005-07-08 17:39:23 -0700679 if (err && err != -EADDRINUSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 break;
681 omode = MCAST_INCLUDE;
682 add = 1;
David L Stevens8cdaaa12005-07-08 17:39:23 -0700683 } else /* IP_DROP_SOURCE_MEMBERSHIP */ {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 omode = MCAST_INCLUDE;
685 add = 0;
686 }
687 err = ip_mc_source(add, omode, sk, &mreqs, 0);
688 break;
689 }
690 case MCAST_JOIN_GROUP:
691 case MCAST_LEAVE_GROUP:
692 {
693 struct group_req greq;
694 struct sockaddr_in *psin;
695 struct ip_mreqn mreq;
696
697 if (optlen < sizeof(struct group_req))
698 goto e_inval;
699 err = -EFAULT;
700 if(copy_from_user(&greq, optval, sizeof(greq)))
701 break;
702 psin = (struct sockaddr_in *)&greq.gr_group;
703 if (psin->sin_family != AF_INET)
704 goto e_inval;
705 memset(&mreq, 0, sizeof(mreq));
706 mreq.imr_multiaddr = psin->sin_addr;
707 mreq.imr_ifindex = greq.gr_interface;
708
709 if (optname == MCAST_JOIN_GROUP)
710 err = ip_mc_join_group(sk, &mreq);
711 else
712 err = ip_mc_leave_group(sk, &mreq);
713 break;
714 }
715 case MCAST_JOIN_SOURCE_GROUP:
716 case MCAST_LEAVE_SOURCE_GROUP:
717 case MCAST_BLOCK_SOURCE:
718 case MCAST_UNBLOCK_SOURCE:
719 {
720 struct group_source_req greqs;
721 struct ip_mreq_source mreqs;
722 struct sockaddr_in *psin;
723 int omode, add;
724
725 if (optlen != sizeof(struct group_source_req))
726 goto e_inval;
727 if (copy_from_user(&greqs, optval, sizeof(greqs))) {
728 err = -EFAULT;
729 break;
730 }
731 if (greqs.gsr_group.ss_family != AF_INET ||
732 greqs.gsr_source.ss_family != AF_INET) {
733 err = -EADDRNOTAVAIL;
734 break;
735 }
736 psin = (struct sockaddr_in *)&greqs.gsr_group;
737 mreqs.imr_multiaddr = psin->sin_addr.s_addr;
738 psin = (struct sockaddr_in *)&greqs.gsr_source;
739 mreqs.imr_sourceaddr = psin->sin_addr.s_addr;
740 mreqs.imr_interface = 0; /* use index for mc_source */
741
742 if (optname == MCAST_BLOCK_SOURCE) {
743 omode = MCAST_EXCLUDE;
744 add = 1;
745 } else if (optname == MCAST_UNBLOCK_SOURCE) {
746 omode = MCAST_EXCLUDE;
747 add = 0;
748 } else if (optname == MCAST_JOIN_SOURCE_GROUP) {
749 struct ip_mreqn mreq;
750
751 psin = (struct sockaddr_in *)&greqs.gsr_group;
752 mreq.imr_multiaddr = psin->sin_addr;
753 mreq.imr_address.s_addr = 0;
754 mreq.imr_ifindex = greqs.gsr_interface;
755 err = ip_mc_join_group(sk, &mreq);
David L Stevens8cdaaa12005-07-08 17:39:23 -0700756 if (err && err != -EADDRINUSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 break;
758 greqs.gsr_interface = mreq.imr_ifindex;
759 omode = MCAST_INCLUDE;
760 add = 1;
761 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
762 omode = MCAST_INCLUDE;
763 add = 0;
764 }
765 err = ip_mc_source(add, omode, sk, &mreqs,
766 greqs.gsr_interface);
767 break;
768 }
769 case MCAST_MSFILTER:
770 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 extern int sysctl_igmp_max_msf;
772 struct sockaddr_in *psin;
773 struct ip_msfilter *msf = NULL;
774 struct group_filter *gsf = NULL;
775 int msize, i, ifindex;
776
777 if (optlen < GROUP_FILTER_SIZE(0))
778 goto e_inval;
779 if (optlen > sysctl_optmem_max) {
780 err = -ENOBUFS;
781 break;
782 }
783 gsf = (struct group_filter *)kmalloc(optlen,GFP_KERNEL);
784 if (gsf == 0) {
785 err = -ENOBUFS;
786 break;
787 }
788 err = -EFAULT;
789 if (copy_from_user(gsf, optval, optlen)) {
790 goto mc_msf_out;
791 }
792 /* numsrc >= (4G-140)/128 overflow in 32 bits */
793 if (gsf->gf_numsrc >= 0x1ffffff ||
794 gsf->gf_numsrc > sysctl_igmp_max_msf) {
795 err = -ENOBUFS;
796 goto mc_msf_out;
797 }
798 if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) {
799 err = -EINVAL;
800 goto mc_msf_out;
801 }
802 msize = IP_MSFILTER_SIZE(gsf->gf_numsrc);
803 msf = (struct ip_msfilter *)kmalloc(msize,GFP_KERNEL);
804 if (msf == 0) {
805 err = -ENOBUFS;
806 goto mc_msf_out;
807 }
808 ifindex = gsf->gf_interface;
809 psin = (struct sockaddr_in *)&gsf->gf_group;
810 if (psin->sin_family != AF_INET) {
811 err = -EADDRNOTAVAIL;
812 goto mc_msf_out;
813 }
814 msf->imsf_multiaddr = psin->sin_addr.s_addr;
815 msf->imsf_interface = 0;
816 msf->imsf_fmode = gsf->gf_fmode;
817 msf->imsf_numsrc = gsf->gf_numsrc;
818 err = -EADDRNOTAVAIL;
819 for (i=0; i<gsf->gf_numsrc; ++i) {
820 psin = (struct sockaddr_in *)&gsf->gf_slist[i];
821
822 if (psin->sin_family != AF_INET)
823 goto mc_msf_out;
824 msf->imsf_slist[i] = psin->sin_addr.s_addr;
825 }
826 kfree(gsf);
827 gsf = NULL;
828
829 err = ip_mc_msfilter(sk, msf, ifindex);
830mc_msf_out:
831 if (msf)
832 kfree(msf);
833 if (gsf)
834 kfree(gsf);
835 break;
836 }
837 case IP_ROUTER_ALERT:
838 err = ip_ra_control(sk, val ? 1 : 0, NULL);
839 break;
840
841 case IP_FREEBIND:
842 if (optlen<1)
843 goto e_inval;
844 inet->freebind = !!val;
845 break;
846
847 case IP_IPSEC_POLICY:
848 case IP_XFRM_POLICY:
Herbert Xu6fc0b4a2005-08-06 06:33:15 -0700849 err = -EPERM;
850 if (!capable(CAP_NET_ADMIN))
851 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 err = xfrm_user_policy(sk, optname, optval, optlen);
853 break;
854
855 default:
856#ifdef CONFIG_NETFILTER
857 err = nf_setsockopt(sk, PF_INET, optname, optval,
858 optlen);
859#else
860 err = -ENOPROTOOPT;
861#endif
862 break;
863 }
864 release_sock(sk);
865 return err;
866
867e_inval:
868 release_sock(sk);
869 return -EINVAL;
870}
871
872/*
873 * Get the options. Note for future reference. The GET of IP options gets the
874 * _received_ ones. The set sets the _sent_ ones.
875 */
876
877int ip_getsockopt(struct sock *sk, int level, int optname, char __user *optval, int __user *optlen)
878{
879 struct inet_sock *inet = inet_sk(sk);
880 int val;
881 int len;
882
883 if(level!=SOL_IP)
884 return -EOPNOTSUPP;
885
886#ifdef CONFIG_IP_MROUTE
887 if(optname>=MRT_BASE && optname <=MRT_BASE+10)
888 {
889 return ip_mroute_getsockopt(sk,optname,optval,optlen);
890 }
891#endif
892
893 if(get_user(len,optlen))
894 return -EFAULT;
895 if(len < 0)
896 return -EINVAL;
897
898 lock_sock(sk);
899
900 switch(optname) {
901 case IP_OPTIONS:
902 {
903 unsigned char optbuf[sizeof(struct ip_options)+40];
904 struct ip_options * opt = (struct ip_options*)optbuf;
905 opt->optlen = 0;
906 if (inet->opt)
907 memcpy(optbuf, inet->opt,
908 sizeof(struct ip_options)+
909 inet->opt->optlen);
910 release_sock(sk);
911
912 if (opt->optlen == 0)
913 return put_user(0, optlen);
914
915 ip_options_undo(opt);
916
917 len = min_t(unsigned int, len, opt->optlen);
918 if(put_user(len, optlen))
919 return -EFAULT;
920 if(copy_to_user(optval, opt->__data, len))
921 return -EFAULT;
922 return 0;
923 }
924 case IP_PKTINFO:
925 val = (inet->cmsg_flags & IP_CMSG_PKTINFO) != 0;
926 break;
927 case IP_RECVTTL:
928 val = (inet->cmsg_flags & IP_CMSG_TTL) != 0;
929 break;
930 case IP_RECVTOS:
931 val = (inet->cmsg_flags & IP_CMSG_TOS) != 0;
932 break;
933 case IP_RECVOPTS:
934 val = (inet->cmsg_flags & IP_CMSG_RECVOPTS) != 0;
935 break;
936 case IP_RETOPTS:
937 val = (inet->cmsg_flags & IP_CMSG_RETOPTS) != 0;
938 break;
939 case IP_TOS:
940 val = inet->tos;
941 break;
942 case IP_TTL:
943 val = (inet->uc_ttl == -1 ?
944 sysctl_ip_default_ttl :
945 inet->uc_ttl);
946 break;
947 case IP_HDRINCL:
948 val = inet->hdrincl;
949 break;
950 case IP_MTU_DISCOVER:
951 val = inet->pmtudisc;
952 break;
953 case IP_MTU:
954 {
955 struct dst_entry *dst;
956 val = 0;
957 dst = sk_dst_get(sk);
958 if (dst) {
959 val = dst_mtu(dst);
960 dst_release(dst);
961 }
962 if (!val) {
963 release_sock(sk);
964 return -ENOTCONN;
965 }
966 break;
967 }
968 case IP_RECVERR:
969 val = inet->recverr;
970 break;
971 case IP_MULTICAST_TTL:
972 val = inet->mc_ttl;
973 break;
974 case IP_MULTICAST_LOOP:
975 val = inet->mc_loop;
976 break;
977 case IP_MULTICAST_IF:
978 {
979 struct in_addr addr;
980 len = min_t(unsigned int, len, sizeof(struct in_addr));
981 addr.s_addr = inet->mc_addr;
982 release_sock(sk);
983
984 if(put_user(len, optlen))
985 return -EFAULT;
986 if(copy_to_user(optval, &addr, len))
987 return -EFAULT;
988 return 0;
989 }
990 case IP_MSFILTER:
991 {
992 struct ip_msfilter msf;
993 int err;
994
995 if (len < IP_MSFILTER_SIZE(0)) {
996 release_sock(sk);
997 return -EINVAL;
998 }
999 if (copy_from_user(&msf, optval, IP_MSFILTER_SIZE(0))) {
1000 release_sock(sk);
1001 return -EFAULT;
1002 }
1003 err = ip_mc_msfget(sk, &msf,
1004 (struct ip_msfilter __user *)optval, optlen);
1005 release_sock(sk);
1006 return err;
1007 }
1008 case MCAST_MSFILTER:
1009 {
1010 struct group_filter gsf;
1011 int err;
1012
1013 if (len < GROUP_FILTER_SIZE(0)) {
1014 release_sock(sk);
1015 return -EINVAL;
1016 }
1017 if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0))) {
1018 release_sock(sk);
1019 return -EFAULT;
1020 }
1021 err = ip_mc_gsfget(sk, &gsf,
1022 (struct group_filter __user *)optval, optlen);
1023 release_sock(sk);
1024 return err;
1025 }
1026 case IP_PKTOPTIONS:
1027 {
1028 struct msghdr msg;
1029
1030 release_sock(sk);
1031
1032 if (sk->sk_type != SOCK_STREAM)
1033 return -ENOPROTOOPT;
1034
1035 msg.msg_control = optval;
1036 msg.msg_controllen = len;
1037 msg.msg_flags = 0;
1038
1039 if (inet->cmsg_flags & IP_CMSG_PKTINFO) {
1040 struct in_pktinfo info;
1041
1042 info.ipi_addr.s_addr = inet->rcv_saddr;
1043 info.ipi_spec_dst.s_addr = inet->rcv_saddr;
1044 info.ipi_ifindex = inet->mc_index;
1045 put_cmsg(&msg, SOL_IP, IP_PKTINFO, sizeof(info), &info);
1046 }
1047 if (inet->cmsg_flags & IP_CMSG_TTL) {
1048 int hlim = inet->mc_ttl;
1049 put_cmsg(&msg, SOL_IP, IP_TTL, sizeof(hlim), &hlim);
1050 }
1051 len -= msg.msg_controllen;
1052 return put_user(len, optlen);
1053 }
1054 case IP_FREEBIND:
1055 val = inet->freebind;
1056 break;
1057 default:
1058#ifdef CONFIG_NETFILTER
1059 val = nf_getsockopt(sk, PF_INET, optname, optval,
1060 &len);
1061 release_sock(sk);
1062 if (val >= 0)
1063 val = put_user(len, optlen);
1064 return val;
1065#else
1066 release_sock(sk);
1067 return -ENOPROTOOPT;
1068#endif
1069 }
1070 release_sock(sk);
1071
1072 if (len < sizeof(int) && len > 0 && val>=0 && val<255) {
1073 unsigned char ucval = (unsigned char)val;
1074 len = 1;
1075 if(put_user(len, optlen))
1076 return -EFAULT;
1077 if(copy_to_user(optval,&ucval,1))
1078 return -EFAULT;
1079 } else {
1080 len = min_t(unsigned int, sizeof(int), len);
1081 if(put_user(len, optlen))
1082 return -EFAULT;
1083 if(copy_to_user(optval,&val,len))
1084 return -EFAULT;
1085 }
1086 return 0;
1087}
1088
1089EXPORT_SYMBOL(ip_cmsg_recv);
1090
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091EXPORT_SYMBOL(ip_getsockopt);
1092EXPORT_SYMBOL(ip_setsockopt);