Import 2.3.18pre1
[davej-history.git] / net / ipv4 / ip_sockglue.c
blob7278a0b4a1feb2fa9ad4319460dd07cd729156c8
1 /*
2 * INET An implementation of the TCP/IP protocol suite for the LINUX
3 * operating system. INET is implemented using the BSD Socket
4 * interface as the means of communication with the user level.
6 * The IP to API glue.
7 *
8 * Version: $Id: ip_sockglue.c,v 1.45 1999/09/06 04:58:03 davem Exp $
10 * Authors: see ip.c
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
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
43 #include <linux/errqueue.h>
44 #include <asm/uaccess.h>
46 #define MAX(a,b) ((a)>(b)?(a):(b))
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
55 * SOL_IP control messages.
58 static void ip_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb)
60 struct in_pktinfo info;
61 struct rtable *rt = (struct rtable *)skb->dst;
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;
72 put_cmsg(msg, SOL_IP, IP_PKTINFO, sizeof(info), &info);
75 static void ip_cmsg_recv_ttl(struct msghdr *msg, struct sk_buff *skb)
77 int ttl = skb->nh.iph->ttl;
78 put_cmsg(msg, SOL_IP, IP_TTL, sizeof(int), &ttl);
81 static void ip_cmsg_recv_tos(struct msghdr *msg, struct sk_buff *skb)
83 put_cmsg(msg, SOL_IP, IP_TOS, 1, &skb->nh.iph->tos);
86 static void ip_cmsg_recv_opts(struct msghdr *msg, struct sk_buff *skb)
88 if (IPCB(skb)->opt.optlen == 0)
89 return;
91 put_cmsg(msg, SOL_IP, IP_RECVOPTS, IPCB(skb)->opt.optlen, skb->nh.iph+1);
95 void ip_cmsg_recv_retopts(struct msghdr *msg, struct sk_buff *skb)
97 unsigned char optbuf[sizeof(struct ip_options) + 40];
98 struct ip_options * opt = (struct ip_options*)optbuf;
100 if (IPCB(skb)->opt.optlen == 0)
101 return;
103 if (ip_options_echo(opt, skb)) {
104 msg->msg_flags |= MSG_CTRUNC;
105 return;
107 ip_options_undo(opt);
109 put_cmsg(msg, SOL_IP, IP_RETOPTS, opt->optlen, opt->__data);
113 void ip_cmsg_recv(struct msghdr *msg, struct sk_buff *skb)
115 unsigned flags = skb->sk->protinfo.af_inet.cmsg_flags;
117 /* Ordered by supposed usage frequency */
118 if (flags & 1)
119 ip_cmsg_recv_pktinfo(msg, skb);
120 if ((flags>>=1) == 0)
121 return;
123 if (flags & 1)
124 ip_cmsg_recv_ttl(msg, skb);
125 if ((flags>>=1) == 0)
126 return;
128 if (flags & 1)
129 ip_cmsg_recv_tos(msg, skb);
130 if ((flags>>=1) == 0)
131 return;
133 if (flags & 1)
134 ip_cmsg_recv_opts(msg, skb);
135 if ((flags>>=1) == 0)
136 return;
138 if (flags & 1)
139 ip_cmsg_recv_retopts(msg, skb);
142 int ip_cmsg_send(struct msghdr *msg, struct ipcm_cookie *ipc)
144 int err;
145 struct cmsghdr *cmsg;
147 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
148 if (cmsg->cmsg_len < sizeof(struct cmsghdr) ||
149 (unsigned long)(((char*)cmsg - (char*)msg->msg_control)
150 + cmsg->cmsg_len) > msg->msg_controllen) {
151 return -EINVAL;
153 if (cmsg->cmsg_level != SOL_IP)
154 continue;
155 switch (cmsg->cmsg_type) {
156 case IP_RETOPTS:
157 err = cmsg->cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr));
158 err = ip_options_get(&ipc->opt, CMSG_DATA(cmsg), err < 40 ? err : 40, 0);
159 if (err)
160 return err;
161 break;
162 case IP_PKTINFO:
164 struct in_pktinfo *info;
165 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct in_pktinfo)))
166 return -EINVAL;
167 info = (struct in_pktinfo *)CMSG_DATA(cmsg);
168 ipc->oif = info->ipi_ifindex;
169 ipc->addr = info->ipi_spec_dst.s_addr;
170 break;
172 default:
173 return -EINVAL;
176 return 0;
180 /* Special input handler for packets catched by router alert option.
181 They are selected only by protocol field, and then processed likely
182 local ones; but only if someone wants them! Otherwise, router
183 not running rsvpd will kill RSVP.
185 It is user level problem, what it will make with them.
186 I have no idea, how it will masquearde or NAT them (it is joke, joke :-)),
187 but receiver should be enough clever f.e. to forward mtrace requests,
188 sent to multicast group to reach destination designated router.
190 struct ip_ra_chain *ip_ra_chain;
191 rwlock_t ip_ra_lock = RW_LOCK_UNLOCKED;
193 int ip_ra_control(struct sock *sk, unsigned char on, void (*destructor)(struct sock *))
195 struct ip_ra_chain *ra, *new_ra, **rap;
197 if (sk->type != SOCK_RAW || sk->num == IPPROTO_RAW)
198 return -EINVAL;
200 new_ra = on ? kmalloc(sizeof(*new_ra), GFP_KERNEL) : NULL;
202 write_lock_bh(&ip_ra_lock);
203 for (rap = &ip_ra_chain; (ra=*rap) != NULL; rap = &ra->next) {
204 if (ra->sk == sk) {
205 if (on) {
206 write_unlock_bh(&ip_ra_lock);
207 if (new_ra)
208 kfree(new_ra);
209 return -EADDRINUSE;
211 *rap = ra->next;
212 write_unlock_bh(&ip_ra_lock);
214 if (ra->destructor)
215 ra->destructor(sk);
216 sock_put(sk);
217 kfree(ra);
218 return 0;
221 if (new_ra == NULL) {
222 write_unlock_bh(&ip_ra_lock);
223 return -ENOBUFS;
225 new_ra->sk = sk;
226 new_ra->destructor = destructor;
228 new_ra->next = ra;
229 *rap = new_ra;
230 sock_hold(sk);
231 write_unlock_bh(&ip_ra_lock);
233 return 0;
236 void ip_icmp_error(struct sock *sk, struct sk_buff *skb, int err,
237 u16 port, u32 info, u8 *payload)
239 struct sock_exterr_skb *serr;
241 if (!sk->protinfo.af_inet.recverr)
242 return;
244 skb = skb_clone(skb, GFP_ATOMIC);
245 if (!skb)
246 return;
248 serr = SKB_EXT_ERR(skb);
249 serr->ee.ee_errno = err;
250 serr->ee.ee_origin = SO_EE_ORIGIN_ICMP;
251 serr->ee.ee_type = skb->h.icmph->type;
252 serr->ee.ee_code = skb->h.icmph->code;
253 serr->ee.ee_pad = 0;
254 serr->ee.ee_info = info;
255 serr->ee.ee_data = 0;
256 serr->addr_offset = (u8*)&(((struct iphdr*)(skb->h.icmph+1))->daddr) - skb->nh.raw;
257 serr->port = port;
259 skb->h.raw = payload;
260 skb_pull(skb, payload - skb->data);
262 if (sock_queue_err_skb(sk, skb))
263 kfree_skb(skb);
266 void ip_local_error(struct sock *sk, int err, u32 daddr, u16 port, u32 info)
268 struct sock_exterr_skb *serr;
269 struct iphdr *iph;
270 struct sk_buff *skb;
272 if (!sk->protinfo.af_inet.recverr)
273 return;
275 skb = alloc_skb(sizeof(struct iphdr), GFP_ATOMIC);
276 if (!skb)
277 return;
279 iph = (struct iphdr*)skb_put(skb, sizeof(struct iphdr));
280 skb->nh.iph = iph;
281 iph->daddr = daddr;
283 serr = SKB_EXT_ERR(skb);
284 serr->ee.ee_errno = err;
285 serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL;
286 serr->ee.ee_type = 0;
287 serr->ee.ee_code = 0;
288 serr->ee.ee_pad = 0;
289 serr->ee.ee_info = info;
290 serr->ee.ee_data = 0;
291 serr->addr_offset = (u8*)&iph->daddr - skb->nh.raw;
292 serr->port = port;
294 skb->h.raw = skb->tail;
295 skb_pull(skb, skb->tail - skb->data);
297 if (sock_queue_err_skb(sk, skb))
298 kfree_skb(skb);
302 * Handle MSG_ERRQUEUE
304 int ip_recv_error(struct sock *sk, struct msghdr *msg, int len)
306 struct sock_exterr_skb *serr;
307 struct sk_buff *skb, *skb2;
308 struct sockaddr_in *sin;
309 struct {
310 struct sock_extended_err ee;
311 struct sockaddr_in offender;
312 } errhdr;
313 int err;
314 int copied;
316 err = -EAGAIN;
317 skb = skb_dequeue(&sk->error_queue);
318 if (skb == NULL)
319 goto out;
321 copied = skb->len;
322 if (copied > len) {
323 msg->msg_flags |= MSG_TRUNC;
324 copied = len;
326 err = memcpy_toiovec(msg->msg_iov, skb->data, copied);
327 if (err)
328 goto out_free_skb;
330 serr = SKB_EXT_ERR(skb);
332 sin = (struct sockaddr_in *)msg->msg_name;
333 if (sin) {
334 sin->sin_family = AF_INET;
335 sin->sin_addr.s_addr = *(u32*)(skb->nh.raw + serr->addr_offset);
336 sin->sin_port = serr->port;
339 memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err));
340 sin = &errhdr.offender;
341 sin->sin_family = AF_UNSPEC;
342 if (serr->ee.ee_origin == SO_EE_ORIGIN_ICMP) {
343 sin->sin_family = AF_INET;
344 sin->sin_addr.s_addr = skb->nh.iph->saddr;
345 if (sk->protinfo.af_inet.cmsg_flags)
346 ip_cmsg_recv(msg, skb);
349 put_cmsg(msg, SOL_IP, IP_RECVERR, sizeof(errhdr), &errhdr);
351 /* Now we could try to dump offended packet options */
353 msg->msg_flags |= MSG_ERRQUEUE;
354 err = copied;
356 /* Reset and regenerate socket error */
357 sk->err = 0;
358 if ((skb2 = skb_peek(&sk->error_queue)) != NULL) {
359 sk->err = SKB_EXT_ERR(skb2)->ee.ee_errno;
360 sk->error_report(sk);
363 out_free_skb:
364 kfree_skb(skb);
365 out:
366 return err;
371 * Socket option code for IP. This is the end of the line after any TCP,UDP etc options on
372 * an IP socket.
374 * We implement IP_TOS (type of service), IP_TTL (time to live).
377 int ip_setsockopt(struct sock *sk, int level, int optname, char *optval, int optlen)
379 int val=0,err;
381 if(optlen>=sizeof(int)) {
382 if(get_user(val, (int *) optval))
383 return -EFAULT;
384 } else if(optlen>=sizeof(char)) {
385 unsigned char ucval;
386 if(get_user(ucval, (unsigned char *) optval))
387 return -EFAULT;
388 val = (int)ucval;
390 /* If optlen==0, it is equivalent to val == 0 */
392 if(level!=SOL_IP)
393 return -ENOPROTOOPT;
394 #ifdef CONFIG_IP_MROUTE
395 if(optname>=MRT_BASE && optname <=MRT_BASE+10)
397 return ip_mroute_setsockopt(sk,optname,optval,optlen);
399 #endif
401 err = 0;
402 lock_sock(sk);
404 switch(optname)
406 case IP_OPTIONS:
408 struct ip_options * opt = NULL;
409 if (optlen > 40 || optlen < 0)
410 goto e_inval;
411 err = ip_options_get(&opt, optval, optlen, 1);
412 if (err)
413 break;
414 if (sk->type == SOCK_STREAM) {
415 struct tcp_opt *tp = &sk->tp_pinfo.af_tcp;
416 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
417 if (sk->family == PF_INET ||
418 ((tcp_connected(sk->state) || sk->state == TCP_SYN_SENT)
419 && sk->daddr != LOOPBACK4_IPV6)) {
420 #endif
421 if (opt)
422 tp->ext_header_len = opt->optlen;
423 tcp_sync_mss(sk, tp->pmtu_cookie);
424 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
426 #endif
428 opt = xchg(&sk->protinfo.af_inet.opt, opt);
429 if (opt)
430 kfree_s(opt, sizeof(struct ip_options) + opt->optlen);
431 break;
433 case IP_PKTINFO:
434 if (val)
435 sk->protinfo.af_inet.cmsg_flags |= IP_CMSG_PKTINFO;
436 else
437 sk->protinfo.af_inet.cmsg_flags &= ~IP_CMSG_PKTINFO;
438 break;
439 case IP_RECVTTL:
440 if (val)
441 sk->protinfo.af_inet.cmsg_flags |= IP_CMSG_TTL;
442 else
443 sk->protinfo.af_inet.cmsg_flags &= ~IP_CMSG_TTL;
444 break;
445 case IP_RECVTOS:
446 if (val)
447 sk->protinfo.af_inet.cmsg_flags |= IP_CMSG_TOS;
448 else
449 sk->protinfo.af_inet.cmsg_flags &= ~IP_CMSG_TOS;
450 break;
451 case IP_RECVOPTS:
452 if (val)
453 sk->protinfo.af_inet.cmsg_flags |= IP_CMSG_RECVOPTS;
454 else
455 sk->protinfo.af_inet.cmsg_flags &= ~IP_CMSG_RECVOPTS;
456 break;
457 case IP_RETOPTS:
458 if (val)
459 sk->protinfo.af_inet.cmsg_flags |= IP_CMSG_RETOPTS;
460 else
461 sk->protinfo.af_inet.cmsg_flags &= ~IP_CMSG_RETOPTS;
462 break;
463 case IP_TOS: /* This sets both TOS and Precedence */
464 /* Reject setting of unused bits */
465 if (val & ~(IPTOS_TOS_MASK|IPTOS_PREC_MASK))
466 goto e_inval;
467 if (IPTOS_PREC(val) >= IPTOS_PREC_CRITIC_ECP &&
468 !capable(CAP_NET_ADMIN)) {
469 err = -EPERM;
470 break;
472 if (sk->protinfo.af_inet.tos != val) {
473 sk->protinfo.af_inet.tos=val;
474 sk->priority = rt_tos2priority(val);
475 sk_dst_reset(sk);
477 break;
478 case IP_TTL:
479 if (optlen<1)
480 goto e_inval;
481 if(val==-1)
482 val = ip_statistics.IpDefaultTTL;
483 if(val<1||val>255)
484 goto e_inval;
485 sk->protinfo.af_inet.ttl=val;
486 break;
487 case IP_HDRINCL:
488 if(sk->type!=SOCK_RAW) {
489 err = -ENOPROTOOPT;
490 break;
492 sk->protinfo.af_inet.hdrincl=val?1:0;
493 break;
494 case IP_MTU_DISCOVER:
495 if (val<0 || val>2)
496 goto e_inval;
497 sk->protinfo.af_inet.pmtudisc = val;
498 break;
499 case IP_RECVERR:
500 sk->protinfo.af_inet.recverr = !!val;
501 if (!val)
502 skb_queue_purge(&sk->error_queue);
503 break;
504 case IP_MULTICAST_TTL:
505 if (sk->type == SOCK_STREAM)
506 goto e_inval;
507 if (optlen<1)
508 goto e_inval;
509 if (val==-1)
510 val = 1;
511 if (val < 0 || val > 255)
512 goto e_inval;
513 sk->protinfo.af_inet.mc_ttl=val;
514 break;
515 case IP_MULTICAST_LOOP:
516 if (optlen<1)
517 goto e_inval;
518 sk->protinfo.af_inet.mc_loop = val ? 1 : 0;
519 break;
520 case IP_MULTICAST_IF:
522 struct ip_mreqn mreq;
523 struct net_device *dev = NULL;
525 if (sk->type == SOCK_STREAM)
526 goto e_inval;
528 * Check the arguments are allowable
531 err = -EFAULT;
532 if (optlen >= sizeof(struct ip_mreqn)) {
533 if (copy_from_user(&mreq,optval,sizeof(mreq)))
534 break;
535 } else {
536 memset(&mreq, 0, sizeof(mreq));
537 if (optlen >= sizeof(struct in_addr) &&
538 copy_from_user(&mreq.imr_address,optval,sizeof(struct in_addr)))
539 break;
542 if (!mreq.imr_ifindex) {
543 if (mreq.imr_address.s_addr == INADDR_ANY) {
544 sk->protinfo.af_inet.mc_index = 0;
545 sk->protinfo.af_inet.mc_addr = 0;
546 err = 0;
547 break;
549 dev = ip_dev_find(mreq.imr_address.s_addr);
550 if (dev) {
551 mreq.imr_ifindex = dev->ifindex;
552 dev_put(dev);
554 } else
555 dev = __dev_get_by_index(mreq.imr_ifindex);
558 err = -EADDRNOTAVAIL;
559 if (!dev)
560 break;
562 err = -EINVAL;
563 if (sk->bound_dev_if && mreq.imr_ifindex != sk->bound_dev_if)
564 break;
566 sk->protinfo.af_inet.mc_index = mreq.imr_ifindex;
567 sk->protinfo.af_inet.mc_addr = mreq.imr_address.s_addr;
568 err = 0;
569 break;
572 case IP_ADD_MEMBERSHIP:
573 case IP_DROP_MEMBERSHIP:
575 struct ip_mreqn mreq;
577 if (optlen < sizeof(struct ip_mreq))
578 goto e_inval;
579 err = -EFAULT;
580 if (optlen >= sizeof(struct ip_mreqn)) {
581 if(copy_from_user(&mreq,optval,sizeof(mreq)))
582 break;
583 } else {
584 memset(&mreq, 0, sizeof(mreq));
585 if (copy_from_user(&mreq,optval,sizeof(struct ip_mreq)))
586 break;
589 if (optname == IP_ADD_MEMBERSHIP)
590 err = ip_mc_join_group(sk,&mreq);
591 else
592 err = ip_mc_leave_group(sk,&mreq);
593 break;
595 case IP_ROUTER_ALERT:
596 err = ip_ra_control(sk, val ? 1 : 0, NULL);
597 break;
599 default:
600 #ifdef CONFIG_NETFILTER
601 err = nf_setsockopt(sk, PF_INET, optname, optval,
602 optlen);
603 #else
604 err = -ENOPROTOOPT;
605 #endif
606 break;
608 release_sock(sk);
609 return err;
611 e_inval:
612 release_sock(sk);
613 return -EINVAL;
617 * Get the options. Note for future reference. The GET of IP options gets the
618 * _received_ ones. The set sets the _sent_ ones.
621 int ip_getsockopt(struct sock *sk, int level, int optname, char *optval, int *optlen)
623 int val;
624 int len;
626 if(level!=SOL_IP)
627 return -EOPNOTSUPP;
629 #ifdef CONFIG_IP_MROUTE
630 if(optname>=MRT_BASE && optname <=MRT_BASE+10)
632 return ip_mroute_getsockopt(sk,optname,optval,optlen);
634 #endif
636 if(get_user(len,optlen))
637 return -EFAULT;
639 lock_sock(sk);
641 switch(optname) {
642 case IP_OPTIONS:
644 unsigned char optbuf[sizeof(struct ip_options)+40];
645 struct ip_options * opt = (struct ip_options*)optbuf;
646 opt->optlen = 0;
647 if (sk->protinfo.af_inet.opt)
648 memcpy(optbuf, sk->protinfo.af_inet.opt,
649 sizeof(struct ip_options)+
650 sk->protinfo.af_inet.opt->optlen);
651 release_sock(sk);
653 if (opt->optlen == 0)
654 return put_user(0, optlen);
656 ip_options_undo(opt);
658 len=min(len, opt->optlen);
659 if(put_user(len, optlen))
660 return -EFAULT;
661 if(copy_to_user(optval, opt->__data, len))
662 return -EFAULT;
663 return 0;
665 case IP_PKTINFO:
666 val = (sk->protinfo.af_inet.cmsg_flags & IP_CMSG_PKTINFO) != 0;
667 break;
668 case IP_RECVTTL:
669 val = (sk->protinfo.af_inet.cmsg_flags & IP_CMSG_TTL) != 0;
670 break;
671 case IP_RECVTOS:
672 val = (sk->protinfo.af_inet.cmsg_flags & IP_CMSG_TOS) != 0;
673 break;
674 case IP_RECVOPTS:
675 val = (sk->protinfo.af_inet.cmsg_flags & IP_CMSG_RECVOPTS) != 0;
676 break;
677 case IP_RETOPTS:
678 val = (sk->protinfo.af_inet.cmsg_flags & IP_CMSG_RETOPTS) != 0;
679 break;
680 case IP_TOS:
681 val=sk->protinfo.af_inet.tos;
682 break;
683 case IP_TTL:
684 val=sk->protinfo.af_inet.ttl;
685 break;
686 case IP_HDRINCL:
687 val=sk->protinfo.af_inet.hdrincl;
688 break;
689 case IP_MTU_DISCOVER:
690 val=sk->protinfo.af_inet.pmtudisc;
691 break;
692 case IP_MTU:
694 struct dst_entry *dst;
695 val = 0;
696 dst = sk_dst_get(sk);
697 if (dst) {
698 val = dst->pmtu;
699 dst_release(dst);
701 if (!val) {
702 release_sock(sk);
703 return -ENOTCONN;
705 break;
707 case IP_RECVERR:
708 val=sk->protinfo.af_inet.recverr;
709 break;
710 case IP_MULTICAST_TTL:
711 val=sk->protinfo.af_inet.mc_ttl;
712 break;
713 case IP_MULTICAST_LOOP:
714 val=sk->protinfo.af_inet.mc_loop;
715 break;
716 case IP_MULTICAST_IF:
718 struct ip_mreqn mreq;
719 len = min(len,sizeof(struct ip_mreqn));
720 mreq.imr_ifindex = sk->protinfo.af_inet.mc_index;
721 mreq.imr_address.s_addr = sk->protinfo.af_inet.mc_addr;
722 mreq.imr_multiaddr.s_addr = 0;
723 release_sock(sk);
725 if(put_user(len, optlen))
726 return -EFAULT;
727 if(copy_to_user((void *)optval, &mreq, len))
728 return -EFAULT;
729 return 0;
731 case IP_PKTOPTIONS:
733 struct msghdr msg;
735 release_sock(sk);
737 if (sk->type != SOCK_STREAM)
738 return -ENOPROTOOPT;
740 msg.msg_control = optval;
741 msg.msg_controllen = len;
742 msg.msg_flags = 0;
744 if (sk->protinfo.af_inet.cmsg_flags&IP_CMSG_PKTINFO) {
745 struct in_pktinfo info;
747 info.ipi_addr.s_addr = sk->rcv_saddr;
748 info.ipi_spec_dst.s_addr = sk->rcv_saddr;
749 info.ipi_ifindex = sk->protinfo.af_inet.mc_index;
750 put_cmsg(&msg, SOL_IP, IP_PKTINFO, sizeof(info), &info);
752 if (sk->protinfo.af_inet.cmsg_flags&IP_CMSG_TTL) {
753 int hlim = sk->protinfo.af_inet.mc_ttl;
754 put_cmsg(&msg, SOL_IP, IP_TTL, sizeof(hlim), &hlim);
756 len -= msg.msg_controllen;
757 return put_user(len, optlen);
759 default:
760 #ifdef CONFIG_NETFILTER
761 val = nf_getsockopt(sk, PF_INET, optname, optval,
762 &len);
763 release_sock(sk);
764 if (val >= 0)
765 val = put_user(len, optlen);
766 return val;
767 #else
768 release_sock(sk);
769 return -ENOPROTOOPT;
770 #endif
772 release_sock(sk);
774 if (len < sizeof(int) && len > 0 && val>=0 && val<255) {
775 unsigned char ucval = (unsigned char)val;
776 len = 1;
777 if(put_user(len, optlen))
778 return -EFAULT;
779 if(copy_to_user(optval,&ucval,1))
780 return -EFAULT;
781 } else {
782 len=min(sizeof(int),len);
783 if(put_user(len, optlen))
784 return -EFAULT;
785 if(copy_to_user(optval,&val,len))
786 return -EFAULT;
788 return 0;