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