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