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.
8 * Version: $Id: ip_sockglue.c,v 1.39 1998/10/03 09:37:33 davem Exp $
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
17 * Mike McLagan : Routing by source
20 #include <linux/config.h>
21 #include <linux/types.h>
23 #include <linux/sched.h>
24 #include <linux/skbuff.h>
26 #include <linux/icmp.h>
27 #include <linux/netdevice.h>
32 #include <linux/tcp.h>
33 #include <linux/udp.h>
34 #include <linux/igmp.h>
35 #include <linux/firewall.h>
36 #include <linux/ip_fw.h>
37 #include <linux/route.h>
38 #include <linux/mroute.h>
39 #include <net/route.h>
40 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
41 #include <net/transp_v6.h>
44 #ifdef CONFIG_IP_MASQUERADE
45 #include <linux/ip_masq.h>
48 #include <linux/errqueue.h>
49 #include <asm/uaccess.h>
51 #define MAX(a,b) ((a)>(b)?(a):(b))
53 #define IP_CMSG_PKTINFO 1
56 #define IP_CMSG_RECVOPTS 8
57 #define IP_CMSG_RETOPTS 16
60 * SOL_IP control messages.
63 static void ip_cmsg_recv_pktinfo(struct msghdr
*msg
, struct sk_buff
*skb
)
65 struct in_pktinfo info
;
66 struct rtable
*rt
= (struct rtable
*)skb
->dst
;
68 info
.ipi_addr
.s_addr
= skb
->nh
.iph
->daddr
;
70 info
.ipi_ifindex
= rt
->rt_iif
;
71 info
.ipi_spec_dst
.s_addr
= rt
->rt_spec_dst
;
74 info
.ipi_spec_dst
.s_addr
= 0;
77 put_cmsg(msg
, SOL_IP
, IP_PKTINFO
, sizeof(info
), &info
);
80 static void ip_cmsg_recv_ttl(struct msghdr
*msg
, struct sk_buff
*skb
)
82 int ttl
= skb
->nh
.iph
->ttl
;
83 put_cmsg(msg
, SOL_IP
, IP_TTL
, sizeof(int), &ttl
);
86 static void ip_cmsg_recv_tos(struct msghdr
*msg
, struct sk_buff
*skb
)
88 put_cmsg(msg
, SOL_IP
, IP_TOS
, 1, &skb
->nh
.iph
->tos
);
91 static void ip_cmsg_recv_opts(struct msghdr
*msg
, struct sk_buff
*skb
)
93 if (IPCB(skb
)->opt
.optlen
== 0)
96 put_cmsg(msg
, SOL_IP
, IP_RECVOPTS
, IPCB(skb
)->opt
.optlen
, skb
->nh
.iph
+1);
100 void ip_cmsg_recv_retopts(struct msghdr
*msg
, struct sk_buff
*skb
)
102 unsigned char optbuf
[sizeof(struct ip_options
) + 40];
103 struct ip_options
* opt
= (struct ip_options
*)optbuf
;
105 if (IPCB(skb
)->opt
.optlen
== 0)
108 if (ip_options_echo(opt
, skb
)) {
109 msg
->msg_flags
|= MSG_CTRUNC
;
112 ip_options_undo(opt
);
114 put_cmsg(msg
, SOL_IP
, IP_RETOPTS
, opt
->optlen
, opt
->__data
);
118 void ip_cmsg_recv(struct msghdr
*msg
, struct sk_buff
*skb
)
120 unsigned flags
= skb
->sk
->ip_cmsg_flags
;
122 /* Ordered by supposed usage frequency */
124 ip_cmsg_recv_pktinfo(msg
, skb
);
125 if ((flags
>>=1) == 0)
129 ip_cmsg_recv_ttl(msg
, skb
);
130 if ((flags
>>=1) == 0)
134 ip_cmsg_recv_tos(msg
, skb
);
135 if ((flags
>>=1) == 0)
139 ip_cmsg_recv_opts(msg
, skb
);
140 if ((flags
>>=1) == 0)
144 ip_cmsg_recv_retopts(msg
, skb
);
147 int ip_cmsg_send(struct msghdr
*msg
, struct ipcm_cookie
*ipc
)
150 struct cmsghdr
*cmsg
;
152 for (cmsg
= CMSG_FIRSTHDR(msg
); cmsg
; cmsg
= CMSG_NXTHDR(msg
, cmsg
)) {
153 if ((unsigned long)(((char*)cmsg
- (char*)msg
->msg_control
)
154 + cmsg
->cmsg_len
) > msg
->msg_controllen
) {
157 if (cmsg
->cmsg_level
!= SOL_IP
)
159 switch (cmsg
->cmsg_type
) {
161 err
= cmsg
->cmsg_len
- CMSG_ALIGN(sizeof(struct cmsghdr
));
162 err
= ip_options_get(&ipc
->opt
, CMSG_DATA(cmsg
), err
< 40 ? err
: 40, 0);
168 struct in_pktinfo
*info
;
169 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof(struct in_pktinfo
)))
171 info
= (struct in_pktinfo
*)CMSG_DATA(cmsg
);
172 ipc
->oif
= info
->ipi_ifindex
;
173 ipc
->addr
= info
->ipi_spec_dst
.s_addr
;
184 /* Special input handler for packets catched by router alert option.
185 They are selected only by protocol field, and then processed likely
186 local ones; but only if someone wants them! Otherwise, router
187 not running rsvpd will kill RSVP.
189 It is user level problem, what it will make with them.
190 I have no idea, how it will masquearde or NAT them (it is joke, joke :-)),
191 but receiver should be enough clever f.e. to forward mtrace requests,
192 sent to multicast group to reach destination designated router.
194 struct ip_ra_chain
*ip_ra_chain
;
196 int ip_ra_control(struct sock
*sk
, unsigned char on
, void (*destructor
)(struct sock
*))
198 struct ip_ra_chain
*ra
, *new_ra
, **rap
;
200 if (sk
->type
!= SOCK_RAW
|| sk
->num
== IPPROTO_RAW
)
203 new_ra
= on
? kmalloc(sizeof(*new_ra
), GFP_KERNEL
) : NULL
;
205 for (rap
= &ip_ra_chain
; (ra
=*rap
) != NULL
; rap
= &ra
->next
) {
222 new_ra
->destructor
= destructor
;
230 void ip_icmp_error(struct sock
*sk
, struct sk_buff
*skb
, int err
,
231 u16 port
, u32 info
, u8
*payload
)
233 struct sock_exterr_skb
*serr
;
238 skb
= skb_clone(skb
, GFP_ATOMIC
);
242 serr
= SKB_EXT_ERR(skb
);
243 serr
->ee
.ee_errno
= err
;
244 serr
->ee
.ee_origin
= SO_EE_ORIGIN_ICMP
;
245 serr
->ee
.ee_type
= skb
->h
.icmph
->type
;
246 serr
->ee
.ee_code
= skb
->h
.icmph
->code
;
248 serr
->ee
.ee_info
= info
;
249 serr
->ee
.ee_data
= 0;
250 serr
->addr_offset
= (u8
*)&(((struct iphdr
*)(skb
->h
.icmph
+1))->daddr
) - skb
->nh
.raw
;
253 skb
->h
.raw
= payload
;
254 skb_pull(skb
, payload
- skb
->data
);
256 if (sock_queue_err_skb(sk
, skb
))
260 void ip_local_error(struct sock
*sk
, int err
, u32 daddr
, u16 port
, u32 info
)
262 struct sock_exterr_skb
*serr
;
269 skb
= alloc_skb(sizeof(struct iphdr
), GFP_ATOMIC
);
273 iph
= (struct iphdr
*)skb_put(skb
, sizeof(struct iphdr
));
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;
283 serr
->ee
.ee_info
= info
;
284 serr
->ee
.ee_data
= 0;
285 serr
->addr_offset
= (u8
*)&iph
->daddr
- skb
->nh
.raw
;
288 skb
->h
.raw
= skb
->tail
;
289 skb_pull(skb
, skb
->tail
- skb
->data
);
291 if (sock_queue_err_skb(sk
, skb
))
296 * Handle MSG_ERRQUEUE
298 int ip_recv_error(struct sock
*sk
, struct msghdr
*msg
, int len
)
300 struct sock_exterr_skb
*serr
;
301 struct sk_buff
*skb
, *skb2
;
302 struct sockaddr_in
*sin
;
304 struct sock_extended_err ee
;
305 struct sockaddr_in offender
;
311 skb
= skb_dequeue(&sk
->error_queue
);
317 msg
->msg_flags
|= MSG_TRUNC
;
320 err
= memcpy_toiovec(msg
->msg_iov
, skb
->data
, copied
);
324 serr
= SKB_EXT_ERR(skb
);
326 sin
= (struct sockaddr_in
*)msg
->msg_name
;
328 sin
->sin_family
= AF_INET
;
329 sin
->sin_addr
.s_addr
= *(u32
*)(skb
->nh
.raw
+ serr
->addr_offset
);
330 sin
->sin_port
= serr
->port
;
333 memcpy(&errhdr
.ee
, &serr
->ee
, sizeof(struct sock_extended_err
));
334 sin
= &errhdr
.offender
;
335 sin
->sin_family
= AF_UNSPEC
;
336 if (serr
->ee
.ee_origin
== SO_EE_ORIGIN_ICMP
) {
337 sin
->sin_family
= AF_INET
;
338 sin
->sin_addr
.s_addr
= skb
->nh
.iph
->saddr
;
339 if (sk
->ip_cmsg_flags
)
340 ip_cmsg_recv(msg
, skb
);
343 put_cmsg(msg
, SOL_IP
, IP_RECVERR
, sizeof(errhdr
), &errhdr
);
345 /* Now we could try to dump offended packet options */
347 msg
->msg_flags
|= MSG_ERRQUEUE
;
350 /* Reset and regenerate socket error */
352 if ((skb2
= skb_peek(&sk
->error_queue
)) != NULL
) {
353 sk
->err
= SKB_EXT_ERR(skb2
)->ee
.ee_errno
;
354 sk
->error_report(sk
);
365 * Socket option code for IP. This is the end of the line after any TCP,UDP etc options on
368 * We implement IP_TOS (type of service), IP_TTL (time to live).
371 int ip_setsockopt(struct sock
*sk
, int level
, int optname
, char *optval
, int optlen
)
374 #if defined(CONFIG_IP_FIREWALL)
375 char tmp_fw
[MAX(sizeof(struct ip_fwtest
),sizeof(struct ip_fwnew
))];
377 if(optlen
>=sizeof(int)) {
378 if(get_user(val
, (int *) optval
))
380 } else if(optlen
>=sizeof(char)) {
382 if(get_user(ucval
, (unsigned char *) optval
))
386 /* If optlen==0, it is equivalent to val == 0 */
390 #ifdef CONFIG_IP_MROUTE
391 if(optname
>=MRT_BASE
&& optname
<=MRT_BASE
+10)
393 return ip_mroute_setsockopt(sk
,optname
,optval
,optlen
);
401 struct ip_options
* opt
= NULL
;
402 if (optlen
> 40 || optlen
< 0)
404 err
= ip_options_get(&opt
, optval
, optlen
, 1);
408 if (sk
->type
== SOCK_STREAM
) {
409 struct tcp_opt
*tp
= &sk
->tp_pinfo
.af_tcp
;
410 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
411 if (sk
->family
== PF_INET
||
412 ((tcp_connected(sk
->state
) || sk
->state
== TCP_SYN_SENT
)
413 && sk
->daddr
!= LOOPBACK4_IPV6
)) {
416 tp
->ext_header_len
= opt
->optlen
;
417 tcp_sync_mss(sk
, tp
->pmtu_cookie
);
418 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
422 opt
= xchg(&sk
->opt
, opt
);
425 kfree_s(opt
, sizeof(struct ip_options
) + opt
->optlen
);
430 sk
->ip_cmsg_flags
|= IP_CMSG_PKTINFO
;
432 sk
->ip_cmsg_flags
&= ~IP_CMSG_PKTINFO
;
436 sk
->ip_cmsg_flags
|= IP_CMSG_TTL
;
438 sk
->ip_cmsg_flags
&= ~IP_CMSG_TTL
;
442 sk
->ip_cmsg_flags
|= IP_CMSG_TOS
;
444 sk
->ip_cmsg_flags
&= ~IP_CMSG_TOS
;
448 sk
->ip_cmsg_flags
|= IP_CMSG_RECVOPTS
;
450 sk
->ip_cmsg_flags
&= ~IP_CMSG_RECVOPTS
;
454 sk
->ip_cmsg_flags
|= IP_CMSG_RETOPTS
;
456 sk
->ip_cmsg_flags
&= ~IP_CMSG_RETOPTS
;
458 case IP_TOS
: /* This sets both TOS and Precedence */
459 /* Reject setting of unused bits */
460 if (val
& ~(IPTOS_TOS_MASK
|IPTOS_PREC_MASK
))
462 if (IPTOS_PREC(val
) >= IPTOS_PREC_CRITIC_ECP
&&
463 !capable(CAP_NET_ADMIN
))
465 if (sk
->ip_tos
!= val
) {
467 sk
->priority
= rt_tos2priority(val
);
468 dst_release(xchg(&sk
->dst_cache
, NULL
));
470 sk
->priority
= rt_tos2priority(val
);
476 val
= ip_statistics
.IpDefaultTTL
;
482 if(sk
->type
!=SOCK_RAW
)
484 sk
->ip_hdrincl
=val
?1:0;
486 case IP_MTU_DISCOVER
:
489 sk
->ip_pmtudisc
= val
;
492 sk
->ip_recverr
= !!val
;
494 skb_queue_purge(&sk
->error_queue
);
496 case IP_MULTICAST_TTL
:
501 if (val
< 0 || val
> 255)
505 case IP_MULTICAST_LOOP
:
508 sk
->ip_mc_loop
= val
? 1 : 0;
510 case IP_MULTICAST_IF
:
512 struct ip_mreqn mreq
;
513 struct device
*dev
= NULL
;
516 * Check the arguments are allowable
519 if (optlen
>= sizeof(struct ip_mreqn
)) {
520 if (copy_from_user(&mreq
,optval
,sizeof(mreq
)))
523 memset(&mreq
, 0, sizeof(mreq
));
524 if (optlen
>= sizeof(struct in_addr
) &&
525 copy_from_user(&mreq
.imr_address
,optval
,sizeof(struct in_addr
)))
529 if (!mreq
.imr_ifindex
) {
530 if (mreq
.imr_address
.s_addr
== INADDR_ANY
) {
535 dev
= ip_dev_find(mreq
.imr_address
.s_addr
);
537 dev
= dev_get_by_index(mreq
.imr_ifindex
);
540 return -EADDRNOTAVAIL
;
542 if (sk
->bound_dev_if
&& dev
->ifindex
!= sk
->bound_dev_if
)
545 sk
->ip_mc_index
= mreq
.imr_ifindex
;
546 sk
->ip_mc_addr
= mreq
.imr_address
.s_addr
;
550 case IP_ADD_MEMBERSHIP
:
551 case IP_DROP_MEMBERSHIP
:
553 struct ip_mreqn mreq
;
555 if (optlen
< sizeof(struct ip_mreq
))
557 if (optlen
>= sizeof(struct ip_mreqn
)) {
558 if(copy_from_user(&mreq
,optval
,sizeof(mreq
)))
561 memset(&mreq
, 0, sizeof(mreq
));
562 if (copy_from_user(&mreq
,optval
,sizeof(struct ip_mreq
)))
566 if (optname
== IP_ADD_MEMBERSHIP
)
567 return ip_mc_join_group(sk
,&mreq
);
569 return ip_mc_leave_group(sk
,&mreq
);
571 case IP_ROUTER_ALERT
:
572 return ip_ra_control(sk
, val
? 1 : 0, NULL
);
574 #ifdef CONFIG_IP_FIREWALL
575 case IP_FW_MASQ_TIMEOUTS
:
579 case IP_FW_DELETE_NUM
:
584 case IP_FW_CREATECHAIN
:
585 case IP_FW_DELETECHAIN
:
587 if(!capable(CAP_NET_ADMIN
))
589 if(optlen
>sizeof(tmp_fw
) || optlen
<1)
591 if(copy_from_user(&tmp_fw
,optval
,optlen
))
593 err
=ip_fw_ctl(optname
, &tmp_fw
,optlen
);
594 return -err
; /* -0 is 0 after all */
595 #endif /* CONFIG_IP_FIREWALL */
596 #ifdef CONFIG_IP_MASQUERADE
598 if(!capable(CAP_NET_ADMIN
))
602 err
=ip_masq_uctl(optname
, optval
,optlen
);
607 return(-ENOPROTOOPT
);
612 * Get the options. Note for future reference. The GET of IP options gets the
613 * _received_ ones. The set sets the _sent_ ones.
616 int ip_getsockopt(struct sock
*sk
, int level
, int optname
, char *optval
, int *optlen
)
624 #ifdef CONFIG_IP_MROUTE
625 if(optname
>=MRT_BASE
&& optname
<=MRT_BASE
+10)
627 return ip_mroute_getsockopt(sk
,optname
,optval
,optlen
);
631 if(get_user(len
,optlen
))
638 unsigned char optbuf
[sizeof(struct ip_options
)+40];
639 struct ip_options
* opt
= (struct ip_options
*)optbuf
;
643 memcpy(optbuf
, sk
->opt
, sizeof(struct ip_options
)+sk
->opt
->optlen
);
645 if (opt
->optlen
== 0)
646 return put_user(0, optlen
);
648 ip_options_undo(opt
);
650 len
=min(len
, opt
->optlen
);
651 if(put_user(len
, optlen
))
653 if(copy_to_user(optval
, opt
->__data
, len
))
658 val
= (sk
->ip_cmsg_flags
& IP_CMSG_PKTINFO
) != 0;
661 val
= (sk
->ip_cmsg_flags
& IP_CMSG_TTL
) != 0;
664 val
= (sk
->ip_cmsg_flags
& IP_CMSG_TOS
) != 0;
667 val
= (sk
->ip_cmsg_flags
& IP_CMSG_RECVOPTS
) != 0;
670 val
= (sk
->ip_cmsg_flags
& IP_CMSG_RETOPTS
) != 0;
681 case IP_MTU_DISCOVER
:
688 val
= sk
->dst_cache
->pmtu
;
696 case IP_MULTICAST_TTL
:
699 case IP_MULTICAST_LOOP
:
702 case IP_MULTICAST_IF
:
704 struct ip_mreqn mreq
;
705 len
= min(len
,sizeof(struct ip_mreqn
));
706 if(put_user(len
, optlen
))
708 mreq
.imr_ifindex
= sk
->ip_mc_index
;
709 mreq
.imr_address
.s_addr
= sk
->ip_mc_addr
;
710 mreq
.imr_multiaddr
.s_addr
= 0;
711 if(copy_to_user((void *)optval
, &mreq
, len
))
716 return(-ENOPROTOOPT
);
719 if (len
< sizeof(int) && len
> 0 && val
>=0 && val
<255) {
720 unsigned char ucval
= (unsigned char)val
;
722 if(put_user(len
, optlen
))
724 if(copy_to_user(optval
,&ucval
,1))
727 len
=min(sizeof(int),len
);
728 if(put_user(len
, optlen
))
730 if(copy_to_user(optval
,&val
,len
))