Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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. |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 7 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * Authors: see ip.c |
| 9 | * |
| 10 | * Fixes: |
| 11 | * Many : Split from ip.c , see ip.c for history. |
| 12 | * Martin Mares : TOS setting fixed. |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 13 | * Alan Cox : Fixed a couple of oopses in Martin's |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | * TOS tweaks. |
| 15 | * Mike McLagan : Routing by source |
| 16 | */ |
| 17 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/module.h> |
| 19 | #include <linux/types.h> |
| 20 | #include <linux/mm.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <linux/skbuff.h> |
| 22 | #include <linux/ip.h> |
| 23 | #include <linux/icmp.h> |
Arnaldo Carvalho de Melo | 14c8502 | 2005-12-27 02:43:12 -0200 | [diff] [blame] | 24 | #include <linux/inetdevice.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include <linux/netdevice.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 26 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include <net/sock.h> |
| 28 | #include <net/ip.h> |
| 29 | #include <net/icmp.h> |
Arnaldo Carvalho de Melo | d83d846 | 2005-12-13 23:26:10 -0800 | [diff] [blame] | 30 | #include <net/tcp_states.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include <linux/udp.h> |
| 32 | #include <linux/igmp.h> |
| 33 | #include <linux/netfilter.h> |
| 34 | #include <linux/route.h> |
| 35 | #include <linux/mroute.h> |
Maciej Żenczykowski | 2c67e9a | 2011-10-22 00:07:47 -0400 | [diff] [blame] | 36 | #include <net/inet_ecn.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #include <net/route.h> |
| 38 | #include <net/xfrm.h> |
David L Stevens | dae5029 | 2008-04-27 01:06:07 -0700 | [diff] [blame] | 39 | #include <net/compat.h> |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 40 | #if IS_ENABLED(CONFIG_IPV6) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #include <net/transp_v6.h> |
| 42 | #endif |
David S. Miller | 35ebf65 | 2012-06-28 03:59:11 -0700 | [diff] [blame] | 43 | #include <net/ip_fib.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |
Catherine Zhang | 2c7946a | 2006-03-20 22:41:23 -0800 | [diff] [blame] | 53 | #define IP_CMSG_PASSSEC 32 |
Balazs Scheidler | e8b2dfe | 2008-11-16 19:32:39 -0800 | [diff] [blame] | 54 | #define IP_CMSG_ORIGDSTADDR 64 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | |
| 56 | /* |
| 57 | * SOL_IP control messages. |
| 58 | */ |
Eric Dumazet | d826eb1 | 2011-11-09 07:24:35 +0000 | [diff] [blame] | 59 | #define PKTINFO_SKB_CB(__skb) ((struct in_pktinfo *)((__skb)->cb)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | |
| 61 | static void ip_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb) |
| 62 | { |
Eric Dumazet | d826eb1 | 2011-11-09 07:24:35 +0000 | [diff] [blame] | 63 | struct in_pktinfo info = *PKTINFO_SKB_CB(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 65 | info.ipi_addr.s_addr = ip_hdr(skb)->daddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | |
| 67 | put_cmsg(msg, SOL_IP, IP_PKTINFO, sizeof(info), &info); |
| 68 | } |
| 69 | |
| 70 | static void ip_cmsg_recv_ttl(struct msghdr *msg, struct sk_buff *skb) |
| 71 | { |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 72 | int ttl = ip_hdr(skb)->ttl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | put_cmsg(msg, SOL_IP, IP_TTL, sizeof(int), &ttl); |
| 74 | } |
| 75 | |
| 76 | static void ip_cmsg_recv_tos(struct msghdr *msg, struct sk_buff *skb) |
| 77 | { |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 78 | put_cmsg(msg, SOL_IP, IP_TOS, 1, &ip_hdr(skb)->tos); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | static void ip_cmsg_recv_opts(struct msghdr *msg, struct sk_buff *skb) |
| 82 | { |
| 83 | if (IPCB(skb)->opt.optlen == 0) |
| 84 | return; |
| 85 | |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 86 | put_cmsg(msg, SOL_IP, IP_RECVOPTS, IPCB(skb)->opt.optlen, |
| 87 | ip_hdr(skb) + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | |
| 91 | static void ip_cmsg_recv_retopts(struct msghdr *msg, struct sk_buff *skb) |
| 92 | { |
| 93 | unsigned char optbuf[sizeof(struct ip_options) + 40]; |
Daniel Baluta | 5e73ea1 | 2012-04-15 01:34:41 +0000 | [diff] [blame] | 94 | struct ip_options *opt = (struct ip_options *)optbuf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | |
| 96 | if (IPCB(skb)->opt.optlen == 0) |
| 97 | return; |
| 98 | |
| 99 | if (ip_options_echo(opt, skb)) { |
| 100 | msg->msg_flags |= MSG_CTRUNC; |
| 101 | return; |
| 102 | } |
| 103 | ip_options_undo(opt); |
| 104 | |
| 105 | put_cmsg(msg, SOL_IP, IP_RETOPTS, opt->optlen, opt->__data); |
| 106 | } |
| 107 | |
Catherine Zhang | 2c7946a | 2006-03-20 22:41:23 -0800 | [diff] [blame] | 108 | static void ip_cmsg_recv_security(struct msghdr *msg, struct sk_buff *skb) |
| 109 | { |
| 110 | char *secdata; |
Catherine Zhang | dc49c1f | 2006-08-02 14:12:06 -0700 | [diff] [blame] | 111 | u32 seclen, secid; |
Catherine Zhang | 2c7946a | 2006-03-20 22:41:23 -0800 | [diff] [blame] | 112 | int err; |
| 113 | |
Catherine Zhang | dc49c1f | 2006-08-02 14:12:06 -0700 | [diff] [blame] | 114 | err = security_socket_getpeersec_dgram(NULL, skb, &secid); |
| 115 | if (err) |
| 116 | return; |
| 117 | |
| 118 | err = security_secid_to_secctx(secid, &secdata, &seclen); |
Catherine Zhang | 2c7946a | 2006-03-20 22:41:23 -0800 | [diff] [blame] | 119 | if (err) |
| 120 | return; |
| 121 | |
| 122 | put_cmsg(msg, SOL_IP, SCM_SECURITY, seclen, secdata); |
Catherine Zhang | dc49c1f | 2006-08-02 14:12:06 -0700 | [diff] [blame] | 123 | security_release_secctx(secdata, seclen); |
Catherine Zhang | 2c7946a | 2006-03-20 22:41:23 -0800 | [diff] [blame] | 124 | } |
| 125 | |
Harvey Harrison | 21d1a16 | 2008-11-20 01:54:27 -0800 | [diff] [blame] | 126 | static void ip_cmsg_recv_dstaddr(struct msghdr *msg, struct sk_buff *skb) |
Balazs Scheidler | e8b2dfe | 2008-11-16 19:32:39 -0800 | [diff] [blame] | 127 | { |
| 128 | struct sockaddr_in sin; |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 129 | const struct iphdr *iph = ip_hdr(skb); |
Harvey Harrison | 21d1a16 | 2008-11-20 01:54:27 -0800 | [diff] [blame] | 130 | __be16 *ports = (__be16 *)skb_transport_header(skb); |
Balazs Scheidler | e8b2dfe | 2008-11-16 19:32:39 -0800 | [diff] [blame] | 131 | |
| 132 | if (skb_transport_offset(skb) + 4 > skb->len) |
| 133 | return; |
| 134 | |
| 135 | /* All current transport protocols have the port numbers in the |
| 136 | * first four bytes of the transport header and this function is |
| 137 | * written with this assumption in mind. |
| 138 | */ |
| 139 | |
| 140 | sin.sin_family = AF_INET; |
| 141 | sin.sin_addr.s_addr = iph->daddr; |
| 142 | sin.sin_port = ports[1]; |
| 143 | memset(sin.sin_zero, 0, sizeof(sin.sin_zero)); |
| 144 | |
| 145 | put_cmsg(msg, SOL_IP, IP_ORIGDSTADDR, sizeof(sin), &sin); |
| 146 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | |
| 148 | void ip_cmsg_recv(struct msghdr *msg, struct sk_buff *skb) |
| 149 | { |
| 150 | struct inet_sock *inet = inet_sk(skb->sk); |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 151 | unsigned int flags = inet->cmsg_flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | |
| 153 | /* Ordered by supposed usage frequency */ |
| 154 | if (flags & 1) |
| 155 | ip_cmsg_recv_pktinfo(msg, skb); |
Eric Dumazet | 4d52cfb | 2009-06-02 00:42:16 -0700 | [diff] [blame] | 156 | if ((flags >>= 1) == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | return; |
| 158 | |
| 159 | if (flags & 1) |
| 160 | ip_cmsg_recv_ttl(msg, skb); |
Eric Dumazet | 4d52cfb | 2009-06-02 00:42:16 -0700 | [diff] [blame] | 161 | if ((flags >>= 1) == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | return; |
| 163 | |
| 164 | if (flags & 1) |
| 165 | ip_cmsg_recv_tos(msg, skb); |
Eric Dumazet | 4d52cfb | 2009-06-02 00:42:16 -0700 | [diff] [blame] | 166 | if ((flags >>= 1) == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | return; |
| 168 | |
| 169 | if (flags & 1) |
| 170 | ip_cmsg_recv_opts(msg, skb); |
Eric Dumazet | 4d52cfb | 2009-06-02 00:42:16 -0700 | [diff] [blame] | 171 | if ((flags >>= 1) == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | return; |
| 173 | |
| 174 | if (flags & 1) |
| 175 | ip_cmsg_recv_retopts(msg, skb); |
Eric Dumazet | 4d52cfb | 2009-06-02 00:42:16 -0700 | [diff] [blame] | 176 | if ((flags >>= 1) == 0) |
Catherine Zhang | 2c7946a | 2006-03-20 22:41:23 -0800 | [diff] [blame] | 177 | return; |
| 178 | |
| 179 | if (flags & 1) |
| 180 | ip_cmsg_recv_security(msg, skb); |
Balazs Scheidler | e8b2dfe | 2008-11-16 19:32:39 -0800 | [diff] [blame] | 181 | |
Eric Dumazet | 4d52cfb | 2009-06-02 00:42:16 -0700 | [diff] [blame] | 182 | if ((flags >>= 1) == 0) |
Balazs Scheidler | e8b2dfe | 2008-11-16 19:32:39 -0800 | [diff] [blame] | 183 | return; |
| 184 | if (flags & 1) |
| 185 | ip_cmsg_recv_dstaddr(msg, skb); |
| 186 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | } |
Eric Dumazet | 4d52cfb | 2009-06-02 00:42:16 -0700 | [diff] [blame] | 188 | EXPORT_SYMBOL(ip_cmsg_recv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | |
Denis V. Lunev | 7a6adb9 | 2008-03-24 15:30:27 -0700 | [diff] [blame] | 190 | int ip_cmsg_send(struct net *net, struct msghdr *msg, struct ipcm_cookie *ipc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | { |
| 192 | int err; |
| 193 | struct cmsghdr *cmsg; |
| 194 | |
| 195 | for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) { |
| 196 | if (!CMSG_OK(msg, cmsg)) |
| 197 | return -EINVAL; |
| 198 | if (cmsg->cmsg_level != SOL_IP) |
| 199 | continue; |
| 200 | switch (cmsg->cmsg_type) { |
| 201 | case IP_RETOPTS: |
| 202 | err = cmsg->cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr)); |
Eric Dumazet | 4d52cfb | 2009-06-02 00:42:16 -0700 | [diff] [blame] | 203 | err = ip_options_get(net, &ipc->opt, CMSG_DATA(cmsg), |
| 204 | err < 40 ? err : 40); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | if (err) |
| 206 | return err; |
| 207 | break; |
| 208 | case IP_PKTINFO: |
| 209 | { |
| 210 | struct in_pktinfo *info; |
| 211 | if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct in_pktinfo))) |
| 212 | return -EINVAL; |
| 213 | info = (struct in_pktinfo *)CMSG_DATA(cmsg); |
| 214 | ipc->oif = info->ipi_ifindex; |
| 215 | ipc->addr = info->ipi_spec_dst.s_addr; |
| 216 | break; |
| 217 | } |
| 218 | default: |
| 219 | return -EINVAL; |
| 220 | } |
| 221 | } |
| 222 | return 0; |
| 223 | } |
| 224 | |
| 225 | |
| 226 | /* Special input handler for packets caught by router alert option. |
| 227 | They are selected only by protocol field, and then processed likely |
| 228 | local ones; but only if someone wants them! Otherwise, router |
| 229 | not running rsvpd will kill RSVP. |
| 230 | |
| 231 | It is user level problem, what it will make with them. |
| 232 | I have no idea, how it will masquearde or NAT them (it is joke, joke :-)), |
| 233 | but receiver should be enough clever f.e. to forward mtrace requests, |
| 234 | sent to multicast group to reach destination designated router. |
| 235 | */ |
Eric Dumazet | 43a951e | 2010-10-25 03:32:44 +0000 | [diff] [blame] | 236 | struct ip_ra_chain __rcu *ip_ra_chain; |
Eric Dumazet | 6601850 | 2010-06-07 03:12:08 +0000 | [diff] [blame] | 237 | static DEFINE_SPINLOCK(ip_ra_lock); |
| 238 | |
Eric Dumazet | 592fcb9 | 2010-06-09 16:21:07 +0000 | [diff] [blame] | 239 | |
| 240 | static void ip_ra_destroy_rcu(struct rcu_head *head) |
Eric Dumazet | 6601850 | 2010-06-07 03:12:08 +0000 | [diff] [blame] | 241 | { |
Eric Dumazet | 592fcb9 | 2010-06-09 16:21:07 +0000 | [diff] [blame] | 242 | struct ip_ra_chain *ra = container_of(head, struct ip_ra_chain, rcu); |
| 243 | |
| 244 | sock_put(ra->saved_sk); |
| 245 | kfree(ra); |
Eric Dumazet | 6601850 | 2010-06-07 03:12:08 +0000 | [diff] [blame] | 246 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | |
Eric Dumazet | 4d52cfb | 2009-06-02 00:42:16 -0700 | [diff] [blame] | 248 | int ip_ra_control(struct sock *sk, unsigned char on, |
| 249 | void (*destructor)(struct sock *)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | { |
Eric Dumazet | 43a951e | 2010-10-25 03:32:44 +0000 | [diff] [blame] | 251 | struct ip_ra_chain *ra, *new_ra; |
| 252 | struct ip_ra_chain __rcu **rap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | |
Eric Dumazet | c720c7e | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 254 | if (sk->sk_type != SOCK_RAW || inet_sk(sk)->inet_num == IPPROTO_RAW) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | return -EINVAL; |
| 256 | |
| 257 | new_ra = on ? kmalloc(sizeof(*new_ra), GFP_KERNEL) : NULL; |
| 258 | |
Eric Dumazet | 6601850 | 2010-06-07 03:12:08 +0000 | [diff] [blame] | 259 | spin_lock_bh(&ip_ra_lock); |
Eric Dumazet | 43a951e | 2010-10-25 03:32:44 +0000 | [diff] [blame] | 260 | for (rap = &ip_ra_chain; |
| 261 | (ra = rcu_dereference_protected(*rap, |
| 262 | lockdep_is_held(&ip_ra_lock))) != NULL; |
| 263 | rap = &ra->next) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | if (ra->sk == sk) { |
| 265 | if (on) { |
Eric Dumazet | 6601850 | 2010-06-07 03:12:08 +0000 | [diff] [blame] | 266 | spin_unlock_bh(&ip_ra_lock); |
Jesper Juhl | a51482b | 2005-11-08 09:41:34 -0800 | [diff] [blame] | 267 | kfree(new_ra); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | return -EADDRINUSE; |
| 269 | } |
Eric Dumazet | 592fcb9 | 2010-06-09 16:21:07 +0000 | [diff] [blame] | 270 | /* dont let ip_call_ra_chain() use sk again */ |
| 271 | ra->sk = NULL; |
Eric Dumazet | 6601850 | 2010-06-07 03:12:08 +0000 | [diff] [blame] | 272 | rcu_assign_pointer(*rap, ra->next); |
| 273 | spin_unlock_bh(&ip_ra_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | |
| 275 | if (ra->destructor) |
| 276 | ra->destructor(sk); |
Eric Dumazet | 592fcb9 | 2010-06-09 16:21:07 +0000 | [diff] [blame] | 277 | /* |
| 278 | * Delay sock_put(sk) and kfree(ra) after one rcu grace |
| 279 | * period. This guarantee ip_call_ra_chain() dont need |
| 280 | * to mess with socket refcounts. |
| 281 | */ |
| 282 | ra->saved_sk = sk; |
| 283 | call_rcu(&ra->rcu, ip_ra_destroy_rcu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | return 0; |
| 285 | } |
| 286 | } |
| 287 | if (new_ra == NULL) { |
Eric Dumazet | 6601850 | 2010-06-07 03:12:08 +0000 | [diff] [blame] | 288 | spin_unlock_bh(&ip_ra_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | return -ENOBUFS; |
| 290 | } |
| 291 | new_ra->sk = sk; |
| 292 | new_ra->destructor = destructor; |
| 293 | |
| 294 | new_ra->next = ra; |
Eric Dumazet | 6601850 | 2010-06-07 03:12:08 +0000 | [diff] [blame] | 295 | rcu_assign_pointer(*rap, new_ra); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | sock_hold(sk); |
Eric Dumazet | 6601850 | 2010-06-07 03:12:08 +0000 | [diff] [blame] | 297 | spin_unlock_bh(&ip_ra_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | |
| 299 | return 0; |
| 300 | } |
| 301 | |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 302 | void ip_icmp_error(struct sock *sk, struct sk_buff *skb, int err, |
Al Viro | 35986b3 | 2006-09-27 18:34:21 -0700 | [diff] [blame] | 303 | __be16 port, u32 info, u8 *payload) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | struct sock_exterr_skb *serr; |
| 306 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | skb = skb_clone(skb, GFP_ATOMIC); |
| 308 | if (!skb) |
| 309 | return; |
| 310 | |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 311 | serr = SKB_EXT_ERR(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | serr->ee.ee_errno = err; |
| 313 | serr->ee.ee_origin = SO_EE_ORIGIN_ICMP; |
Arnaldo Carvalho de Melo | 88c7664 | 2007-03-13 14:43:18 -0300 | [diff] [blame] | 314 | serr->ee.ee_type = icmp_hdr(skb)->type; |
| 315 | serr->ee.ee_code = icmp_hdr(skb)->code; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | serr->ee.ee_pad = 0; |
| 317 | serr->ee.ee_info = info; |
| 318 | serr->ee.ee_data = 0; |
Arnaldo Carvalho de Melo | 88c7664 | 2007-03-13 14:43:18 -0300 | [diff] [blame] | 319 | serr->addr_offset = (u8 *)&(((struct iphdr *)(icmp_hdr(skb) + 1))->daddr) - |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 320 | skb_network_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | serr->port = port; |
| 322 | |
Arnaldo Carvalho de Melo | bd82393 | 2007-03-13 17:10:43 -0300 | [diff] [blame] | 323 | if (skb_pull(skb, payload - skb->data) != NULL) { |
| 324 | skb_reset_transport_header(skb); |
| 325 | if (sock_queue_err_skb(sk, skb) == 0) |
| 326 | return; |
| 327 | } |
| 328 | kfree_skb(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | } |
| 330 | |
Al Viro | 0579016 | 2006-09-27 18:33:40 -0700 | [diff] [blame] | 331 | void ip_local_error(struct sock *sk, int err, __be32 daddr, __be16 port, u32 info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | { |
| 333 | struct inet_sock *inet = inet_sk(sk); |
| 334 | struct sock_exterr_skb *serr; |
| 335 | struct iphdr *iph; |
| 336 | struct sk_buff *skb; |
| 337 | |
| 338 | if (!inet->recverr) |
| 339 | return; |
| 340 | |
| 341 | skb = alloc_skb(sizeof(struct iphdr), GFP_ATOMIC); |
| 342 | if (!skb) |
| 343 | return; |
| 344 | |
Arnaldo Carvalho de Melo | 2ca9e6f | 2007-03-10 19:15:25 -0300 | [diff] [blame] | 345 | skb_put(skb, sizeof(struct iphdr)); |
| 346 | skb_reset_network_header(skb); |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 347 | iph = ip_hdr(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | iph->daddr = daddr; |
| 349 | |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 350 | serr = SKB_EXT_ERR(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | serr->ee.ee_errno = err; |
| 352 | serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL; |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 353 | serr->ee.ee_type = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | serr->ee.ee_code = 0; |
| 355 | serr->ee.ee_pad = 0; |
| 356 | serr->ee.ee_info = info; |
| 357 | serr->ee.ee_data = 0; |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 358 | serr->addr_offset = (u8 *)&iph->daddr - skb_network_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | serr->port = port; |
| 360 | |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 361 | __skb_pull(skb, skb_tail_pointer(skb) - skb->data); |
Arnaldo Carvalho de Melo | bd82393 | 2007-03-13 17:10:43 -0300 | [diff] [blame] | 362 | skb_reset_transport_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | |
| 364 | if (sock_queue_err_skb(sk, skb)) |
| 365 | kfree_skb(skb); |
| 366 | } |
| 367 | |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 368 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | * Handle MSG_ERRQUEUE |
| 370 | */ |
| 371 | int ip_recv_error(struct sock *sk, struct msghdr *msg, int len) |
| 372 | { |
| 373 | struct sock_exterr_skb *serr; |
| 374 | struct sk_buff *skb, *skb2; |
| 375 | struct sockaddr_in *sin; |
| 376 | struct { |
| 377 | struct sock_extended_err ee; |
| 378 | struct sockaddr_in offender; |
| 379 | } errhdr; |
| 380 | int err; |
| 381 | int copied; |
| 382 | |
| 383 | err = -EAGAIN; |
| 384 | skb = skb_dequeue(&sk->sk_error_queue); |
| 385 | if (skb == NULL) |
| 386 | goto out; |
| 387 | |
| 388 | copied = skb->len; |
| 389 | if (copied > len) { |
| 390 | msg->msg_flags |= MSG_TRUNC; |
| 391 | copied = len; |
| 392 | } |
| 393 | err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied); |
| 394 | if (err) |
| 395 | goto out_free_skb; |
| 396 | |
| 397 | sock_recv_timestamp(msg, sk, skb); |
| 398 | |
| 399 | serr = SKB_EXT_ERR(skb); |
| 400 | |
| 401 | sin = (struct sockaddr_in *)msg->msg_name; |
| 402 | if (sin) { |
| 403 | sin->sin_family = AF_INET; |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 404 | sin->sin_addr.s_addr = *(__be32 *)(skb_network_header(skb) + |
| 405 | serr->addr_offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | sin->sin_port = serr->port; |
| 407 | memset(&sin->sin_zero, 0, sizeof(sin->sin_zero)); |
| 408 | } |
| 409 | |
| 410 | memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err)); |
| 411 | sin = &errhdr.offender; |
| 412 | sin->sin_family = AF_UNSPEC; |
| 413 | if (serr->ee.ee_origin == SO_EE_ORIGIN_ICMP) { |
| 414 | struct inet_sock *inet = inet_sk(sk); |
| 415 | |
| 416 | sin->sin_family = AF_INET; |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 417 | sin->sin_addr.s_addr = ip_hdr(skb)->saddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | sin->sin_port = 0; |
| 419 | memset(&sin->sin_zero, 0, sizeof(sin->sin_zero)); |
| 420 | if (inet->cmsg_flags) |
| 421 | ip_cmsg_recv(msg, skb); |
| 422 | } |
| 423 | |
| 424 | put_cmsg(msg, SOL_IP, IP_RECVERR, sizeof(errhdr), &errhdr); |
| 425 | |
| 426 | /* Now we could try to dump offended packet options */ |
| 427 | |
| 428 | msg->msg_flags |= MSG_ERRQUEUE; |
| 429 | err = copied; |
| 430 | |
| 431 | /* Reset and regenerate socket error */ |
Herbert Xu | e0f9f85 | 2005-06-18 22:56:18 -0700 | [diff] [blame] | 432 | spin_lock_bh(&sk->sk_error_queue.lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | sk->sk_err = 0; |
Eric Dumazet | 4d52cfb | 2009-06-02 00:42:16 -0700 | [diff] [blame] | 434 | skb2 = skb_peek(&sk->sk_error_queue); |
| 435 | if (skb2 != NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | sk->sk_err = SKB_EXT_ERR(skb2)->ee.ee_errno; |
Herbert Xu | e0f9f85 | 2005-06-18 22:56:18 -0700 | [diff] [blame] | 437 | spin_unlock_bh(&sk->sk_error_queue.lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | sk->sk_error_report(sk); |
| 439 | } else |
Herbert Xu | e0f9f85 | 2005-06-18 22:56:18 -0700 | [diff] [blame] | 440 | spin_unlock_bh(&sk->sk_error_queue.lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 442 | out_free_skb: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | kfree_skb(skb); |
| 444 | out: |
| 445 | return err; |
| 446 | } |
| 447 | |
| 448 | |
| 449 | /* |
Eric Dumazet | 4d52cfb | 2009-06-02 00:42:16 -0700 | [diff] [blame] | 450 | * Socket option code for IP. This is the end of the line after any |
| 451 | * TCP,UDP etc options on an IP socket. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | */ |
| 453 | |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 454 | static int do_ip_setsockopt(struct sock *sk, int level, |
David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 455 | int optname, char __user *optval, unsigned int optlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | { |
| 457 | struct inet_sock *inet = inet_sk(sk); |
Jianjun Kong | 09cb105 | 2008-11-03 00:27:11 -0800 | [diff] [blame] | 458 | int val = 0, err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 460 | if (((1<<optname) & ((1<<IP_PKTINFO) | (1<<IP_RECVTTL) | |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 461 | (1<<IP_RECVOPTS) | (1<<IP_RECVTOS) | |
| 462 | (1<<IP_RETOPTS) | (1<<IP_TOS) | |
| 463 | (1<<IP_TTL) | (1<<IP_HDRINCL) | |
| 464 | (1<<IP_MTU_DISCOVER) | (1<<IP_RECVERR) | |
| 465 | (1<<IP_ROUTER_ALERT) | (1<<IP_FREEBIND) | |
Stephen Hemminger | d218d11 | 2010-01-11 16:28:01 -0800 | [diff] [blame] | 466 | (1<<IP_PASSSEC) | (1<<IP_TRANSPARENT) | |
Jiri Olsa | 7b2ff18 | 2010-06-15 01:07:31 +0000 | [diff] [blame] | 467 | (1<<IP_MINTTL) | (1<<IP_NODEFRAG))) || |
Erich E. Hoover | 76e2105 | 2012-02-08 09:11:07 +0000 | [diff] [blame] | 468 | optname == IP_UNICAST_IF || |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 469 | optname == IP_MULTICAST_TTL || |
Nivedita Singhvi | f771bef | 2009-05-28 07:00:46 +0000 | [diff] [blame] | 470 | optname == IP_MULTICAST_ALL || |
Balazs Scheidler | e8b2dfe | 2008-11-16 19:32:39 -0800 | [diff] [blame] | 471 | optname == IP_MULTICAST_LOOP || |
| 472 | optname == IP_RECVORIGDSTADDR) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | if (optlen >= sizeof(int)) { |
| 474 | if (get_user(val, (int __user *) optval)) |
| 475 | return -EFAULT; |
| 476 | } else if (optlen >= sizeof(char)) { |
| 477 | unsigned char ucval; |
| 478 | |
| 479 | if (get_user(ucval, (unsigned char __user *) optval)) |
| 480 | return -EFAULT; |
| 481 | val = (int) ucval; |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | /* If optlen==0, it is equivalent to val == 0 */ |
| 486 | |
Pavel Emelyanov | 6a9fb94 | 2007-11-05 21:32:31 -0800 | [diff] [blame] | 487 | if (ip_mroute_opt(optname)) |
Jianjun Kong | 09cb105 | 2008-11-03 00:27:11 -0800 | [diff] [blame] | 488 | return ip_mroute_setsockopt(sk, optname, optval, optlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | |
| 490 | err = 0; |
| 491 | lock_sock(sk); |
| 492 | |
| 493 | switch (optname) { |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 494 | case IP_OPTIONS: |
| 495 | { |
Eric Dumazet | f6d8bd0 | 2011-04-21 09:45:37 +0000 | [diff] [blame] | 496 | struct ip_options_rcu *old, *opt = NULL; |
| 497 | |
roel kluin | 65a1c4f | 2009-10-23 05:59:21 +0000 | [diff] [blame] | 498 | if (optlen > 40) |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 499 | goto e_inval; |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 500 | err = ip_options_get_from_user(sock_net(sk), &opt, |
Denis V. Lunev | cb84663 | 2008-03-24 15:31:00 -0700 | [diff] [blame] | 501 | optval, optlen); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 502 | if (err) |
| 503 | break; |
Eric Dumazet | f6d8bd0 | 2011-04-21 09:45:37 +0000 | [diff] [blame] | 504 | old = rcu_dereference_protected(inet->inet_opt, |
| 505 | sock_owned_by_user(sk)); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 506 | if (inet->is_icsk) { |
| 507 | struct inet_connection_sock *icsk = inet_csk(sk); |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 508 | #if IS_ENABLED(CONFIG_IPV6) |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 509 | if (sk->sk_family == PF_INET || |
| 510 | (!((1 << sk->sk_state) & |
| 511 | (TCPF_LISTEN | TCPF_CLOSE)) && |
Eric Dumazet | c720c7e | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 512 | inet->inet_daddr != LOOPBACK4_IPV6)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | #endif |
Eric Dumazet | f6d8bd0 | 2011-04-21 09:45:37 +0000 | [diff] [blame] | 514 | if (old) |
| 515 | icsk->icsk_ext_hdr_len -= old->opt.optlen; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 516 | if (opt) |
Eric Dumazet | f6d8bd0 | 2011-04-21 09:45:37 +0000 | [diff] [blame] | 517 | icsk->icsk_ext_hdr_len += opt->opt.optlen; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 518 | icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie); |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 519 | #if IS_ENABLED(CONFIG_IPV6) |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 520 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | } |
Eric Dumazet | f6d8bd0 | 2011-04-21 09:45:37 +0000 | [diff] [blame] | 523 | rcu_assign_pointer(inet->inet_opt, opt); |
| 524 | if (old) |
Paul E. McKenney | 605b4af | 2012-01-06 17:08:33 -0800 | [diff] [blame] | 525 | kfree_rcu(old, rcu); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 526 | break; |
| 527 | } |
| 528 | case IP_PKTINFO: |
| 529 | if (val) |
| 530 | inet->cmsg_flags |= IP_CMSG_PKTINFO; |
| 531 | else |
| 532 | inet->cmsg_flags &= ~IP_CMSG_PKTINFO; |
| 533 | break; |
| 534 | case IP_RECVTTL: |
| 535 | if (val) |
| 536 | inet->cmsg_flags |= IP_CMSG_TTL; |
| 537 | else |
| 538 | inet->cmsg_flags &= ~IP_CMSG_TTL; |
| 539 | break; |
| 540 | case IP_RECVTOS: |
| 541 | if (val) |
| 542 | inet->cmsg_flags |= IP_CMSG_TOS; |
| 543 | else |
| 544 | inet->cmsg_flags &= ~IP_CMSG_TOS; |
| 545 | break; |
| 546 | case IP_RECVOPTS: |
| 547 | if (val) |
| 548 | inet->cmsg_flags |= IP_CMSG_RECVOPTS; |
| 549 | else |
| 550 | inet->cmsg_flags &= ~IP_CMSG_RECVOPTS; |
| 551 | break; |
| 552 | case IP_RETOPTS: |
| 553 | if (val) |
| 554 | inet->cmsg_flags |= IP_CMSG_RETOPTS; |
| 555 | else |
| 556 | inet->cmsg_flags &= ~IP_CMSG_RETOPTS; |
| 557 | break; |
| 558 | case IP_PASSSEC: |
| 559 | if (val) |
| 560 | inet->cmsg_flags |= IP_CMSG_PASSSEC; |
| 561 | else |
| 562 | inet->cmsg_flags &= ~IP_CMSG_PASSSEC; |
| 563 | break; |
Balazs Scheidler | e8b2dfe | 2008-11-16 19:32:39 -0800 | [diff] [blame] | 564 | case IP_RECVORIGDSTADDR: |
| 565 | if (val) |
| 566 | inet->cmsg_flags |= IP_CMSG_ORIGDSTADDR; |
| 567 | else |
| 568 | inet->cmsg_flags &= ~IP_CMSG_ORIGDSTADDR; |
| 569 | break; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 570 | case IP_TOS: /* This sets both TOS and Precedence */ |
| 571 | if (sk->sk_type == SOCK_STREAM) { |
Maciej Żenczykowski | 2c67e9a | 2011-10-22 00:07:47 -0400 | [diff] [blame] | 572 | val &= ~INET_ECN_MASK; |
| 573 | val |= inet->tos & INET_ECN_MASK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | } |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 575 | if (inet->tos != val) { |
| 576 | inet->tos = val; |
| 577 | sk->sk_priority = rt_tos2priority(val); |
| 578 | sk_dst_reset(sk); |
| 579 | } |
| 580 | break; |
| 581 | case IP_TTL: |
Eric Dumazet | 4d52cfb | 2009-06-02 00:42:16 -0700 | [diff] [blame] | 582 | if (optlen < 1) |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 583 | goto e_inval; |
Eric Dumazet | 4d52cfb | 2009-06-02 00:42:16 -0700 | [diff] [blame] | 584 | if (val != -1 && (val < 0 || val > 255)) |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 585 | goto e_inval; |
| 586 | inet->uc_ttl = val; |
| 587 | break; |
| 588 | case IP_HDRINCL: |
| 589 | if (sk->sk_type != SOCK_RAW) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | err = -ENOPROTOOPT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | break; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 592 | } |
| 593 | inet->hdrincl = val ? 1 : 0; |
| 594 | break; |
Jiri Olsa | 7b2ff18 | 2010-06-15 01:07:31 +0000 | [diff] [blame] | 595 | case IP_NODEFRAG: |
| 596 | if (sk->sk_type != SOCK_RAW) { |
| 597 | err = -ENOPROTOOPT; |
| 598 | break; |
| 599 | } |
| 600 | inet->nodefrag = val ? 1 : 0; |
| 601 | break; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 602 | case IP_MTU_DISCOVER: |
John Dykstra | 0eae750 | 2009-10-19 21:53:53 -0700 | [diff] [blame] | 603 | if (val < IP_PMTUDISC_DONT || val > IP_PMTUDISC_PROBE) |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 604 | goto e_inval; |
| 605 | inet->pmtudisc = val; |
| 606 | break; |
| 607 | case IP_RECVERR: |
| 608 | inet->recverr = !!val; |
| 609 | if (!val) |
| 610 | skb_queue_purge(&sk->sk_error_queue); |
| 611 | break; |
| 612 | case IP_MULTICAST_TTL: |
| 613 | if (sk->sk_type == SOCK_STREAM) |
| 614 | goto e_inval; |
Eric Dumazet | 4d52cfb | 2009-06-02 00:42:16 -0700 | [diff] [blame] | 615 | if (optlen < 1) |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 616 | goto e_inval; |
Jianjun Kong | 09cb105 | 2008-11-03 00:27:11 -0800 | [diff] [blame] | 617 | if (val == -1) |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 618 | val = 1; |
| 619 | if (val < 0 || val > 255) |
| 620 | goto e_inval; |
| 621 | inet->mc_ttl = val; |
| 622 | break; |
| 623 | case IP_MULTICAST_LOOP: |
Eric Dumazet | 4d52cfb | 2009-06-02 00:42:16 -0700 | [diff] [blame] | 624 | if (optlen < 1) |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 625 | goto e_inval; |
| 626 | inet->mc_loop = !!val; |
| 627 | break; |
Erich E. Hoover | 76e2105 | 2012-02-08 09:11:07 +0000 | [diff] [blame] | 628 | case IP_UNICAST_IF: |
| 629 | { |
| 630 | struct net_device *dev = NULL; |
| 631 | int ifindex; |
| 632 | |
| 633 | if (optlen != sizeof(int)) |
| 634 | goto e_inval; |
| 635 | |
| 636 | ifindex = (__force int)ntohl((__force __be32)val); |
| 637 | if (ifindex == 0) { |
| 638 | inet->uc_index = 0; |
| 639 | err = 0; |
| 640 | break; |
| 641 | } |
| 642 | |
| 643 | dev = dev_get_by_index(sock_net(sk), ifindex); |
| 644 | err = -EADDRNOTAVAIL; |
| 645 | if (!dev) |
| 646 | break; |
| 647 | dev_put(dev); |
| 648 | |
| 649 | err = -EINVAL; |
| 650 | if (sk->sk_bound_dev_if) |
| 651 | break; |
| 652 | |
| 653 | inet->uc_index = ifindex; |
| 654 | err = 0; |
| 655 | break; |
| 656 | } |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 657 | case IP_MULTICAST_IF: |
| 658 | { |
| 659 | struct ip_mreqn mreq; |
| 660 | struct net_device *dev = NULL; |
| 661 | |
| 662 | if (sk->sk_type == SOCK_STREAM) |
| 663 | goto e_inval; |
| 664 | /* |
| 665 | * Check the arguments are allowable |
| 666 | */ |
| 667 | |
Shan Wei | 0915921 | 2009-09-22 15:41:10 +0000 | [diff] [blame] | 668 | if (optlen < sizeof(struct in_addr)) |
| 669 | goto e_inval; |
| 670 | |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 671 | err = -EFAULT; |
| 672 | if (optlen >= sizeof(struct ip_mreqn)) { |
Jianjun Kong | 09cb105 | 2008-11-03 00:27:11 -0800 | [diff] [blame] | 673 | if (copy_from_user(&mreq, optval, sizeof(mreq))) |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 674 | break; |
| 675 | } else { |
| 676 | memset(&mreq, 0, sizeof(mreq)); |
Jiri Pirko | 3a084dd | 2012-05-03 22:37:45 +0000 | [diff] [blame] | 677 | if (optlen >= sizeof(struct ip_mreq)) { |
| 678 | if (copy_from_user(&mreq, optval, |
| 679 | sizeof(struct ip_mreq))) |
| 680 | break; |
| 681 | } else if (optlen >= sizeof(struct in_addr)) { |
| 682 | if (copy_from_user(&mreq.imr_address, optval, |
| 683 | sizeof(struct in_addr))) |
| 684 | break; |
| 685 | } |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 686 | } |
| 687 | |
| 688 | if (!mreq.imr_ifindex) { |
Al Viro | e6f1ceb | 2008-03-17 22:44:53 -0700 | [diff] [blame] | 689 | if (mreq.imr_address.s_addr == htonl(INADDR_ANY)) { |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 690 | inet->mc_index = 0; |
| 691 | inet->mc_addr = 0; |
| 692 | err = 0; |
| 693 | break; |
| 694 | } |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 695 | dev = ip_dev_find(sock_net(sk), mreq.imr_address.s_addr); |
Eric Dumazet | 55b8050 | 2009-10-19 06:41:58 +0000 | [diff] [blame] | 696 | if (dev) |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 697 | mreq.imr_ifindex = dev->ifindex; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 698 | } else |
Eric Dumazet | 55b8050 | 2009-10-19 06:41:58 +0000 | [diff] [blame] | 699 | dev = dev_get_by_index(sock_net(sk), mreq.imr_ifindex); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 700 | |
| 701 | |
| 702 | err = -EADDRNOTAVAIL; |
| 703 | if (!dev) |
| 704 | break; |
Eric Dumazet | 55b8050 | 2009-10-19 06:41:58 +0000 | [diff] [blame] | 705 | dev_put(dev); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 706 | |
| 707 | err = -EINVAL; |
| 708 | if (sk->sk_bound_dev_if && |
| 709 | mreq.imr_ifindex != sk->sk_bound_dev_if) |
| 710 | break; |
| 711 | |
| 712 | inet->mc_index = mreq.imr_ifindex; |
| 713 | inet->mc_addr = mreq.imr_address.s_addr; |
| 714 | err = 0; |
| 715 | break; |
| 716 | } |
| 717 | |
| 718 | case IP_ADD_MEMBERSHIP: |
| 719 | case IP_DROP_MEMBERSHIP: |
| 720 | { |
| 721 | struct ip_mreqn mreq; |
| 722 | |
Flavio Leitner | a96fb49 | 2007-08-24 22:16:39 -0700 | [diff] [blame] | 723 | err = -EPROTO; |
| 724 | if (inet_sk(sk)->is_icsk) |
| 725 | break; |
| 726 | |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 727 | if (optlen < sizeof(struct ip_mreq)) |
| 728 | goto e_inval; |
| 729 | err = -EFAULT; |
| 730 | if (optlen >= sizeof(struct ip_mreqn)) { |
Jianjun Kong | 09cb105 | 2008-11-03 00:27:11 -0800 | [diff] [blame] | 731 | if (copy_from_user(&mreq, optval, sizeof(mreq))) |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 732 | break; |
| 733 | } else { |
| 734 | memset(&mreq, 0, sizeof(mreq)); |
Jianjun Kong | 09cb105 | 2008-11-03 00:27:11 -0800 | [diff] [blame] | 735 | if (copy_from_user(&mreq, optval, sizeof(struct ip_mreq))) |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 736 | break; |
| 737 | } |
| 738 | |
| 739 | if (optname == IP_ADD_MEMBERSHIP) |
| 740 | err = ip_mc_join_group(sk, &mreq); |
| 741 | else |
| 742 | err = ip_mc_leave_group(sk, &mreq); |
| 743 | break; |
| 744 | } |
| 745 | case IP_MSFILTER: |
| 746 | { |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 747 | struct ip_msfilter *msf; |
| 748 | |
| 749 | if (optlen < IP_MSFILTER_SIZE(0)) |
| 750 | goto e_inval; |
| 751 | if (optlen > sysctl_optmem_max) { |
| 752 | err = -ENOBUFS; |
| 753 | break; |
| 754 | } |
| 755 | msf = kmalloc(optlen, GFP_KERNEL); |
Stephen Hemminger | cfcabdc | 2007-10-09 01:59:42 -0700 | [diff] [blame] | 756 | if (!msf) { |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 757 | err = -ENOBUFS; |
| 758 | break; |
| 759 | } |
| 760 | err = -EFAULT; |
| 761 | if (copy_from_user(msf, optval, optlen)) { |
| 762 | kfree(msf); |
| 763 | break; |
| 764 | } |
| 765 | /* numsrc >= (1G-4) overflow in 32 bits */ |
| 766 | if (msf->imsf_numsrc >= 0x3ffffffcU || |
| 767 | msf->imsf_numsrc > sysctl_igmp_max_msf) { |
| 768 | kfree(msf); |
| 769 | err = -ENOBUFS; |
| 770 | break; |
| 771 | } |
| 772 | if (IP_MSFILTER_SIZE(msf->imsf_numsrc) > optlen) { |
| 773 | kfree(msf); |
| 774 | err = -EINVAL; |
| 775 | break; |
| 776 | } |
| 777 | err = ip_mc_msfilter(sk, msf, 0); |
| 778 | kfree(msf); |
| 779 | break; |
| 780 | } |
| 781 | case IP_BLOCK_SOURCE: |
| 782 | case IP_UNBLOCK_SOURCE: |
| 783 | case IP_ADD_SOURCE_MEMBERSHIP: |
| 784 | case IP_DROP_SOURCE_MEMBERSHIP: |
| 785 | { |
| 786 | struct ip_mreq_source mreqs; |
| 787 | int omode, add; |
| 788 | |
| 789 | if (optlen != sizeof(struct ip_mreq_source)) |
| 790 | goto e_inval; |
| 791 | if (copy_from_user(&mreqs, optval, sizeof(mreqs))) { |
| 792 | err = -EFAULT; |
| 793 | break; |
| 794 | } |
| 795 | if (optname == IP_BLOCK_SOURCE) { |
| 796 | omode = MCAST_EXCLUDE; |
| 797 | add = 1; |
| 798 | } else if (optname == IP_UNBLOCK_SOURCE) { |
| 799 | omode = MCAST_EXCLUDE; |
| 800 | add = 0; |
| 801 | } else if (optname == IP_ADD_SOURCE_MEMBERSHIP) { |
| 802 | struct ip_mreqn mreq; |
| 803 | |
| 804 | mreq.imr_multiaddr.s_addr = mreqs.imr_multiaddr; |
| 805 | mreq.imr_address.s_addr = mreqs.imr_interface; |
| 806 | mreq.imr_ifindex = 0; |
| 807 | err = ip_mc_join_group(sk, &mreq); |
| 808 | if (err && err != -EADDRINUSE) |
| 809 | break; |
| 810 | omode = MCAST_INCLUDE; |
| 811 | add = 1; |
| 812 | } else /* IP_DROP_SOURCE_MEMBERSHIP */ { |
| 813 | omode = MCAST_INCLUDE; |
| 814 | add = 0; |
| 815 | } |
| 816 | err = ip_mc_source(add, omode, sk, &mreqs, 0); |
| 817 | break; |
| 818 | } |
| 819 | case MCAST_JOIN_GROUP: |
| 820 | case MCAST_LEAVE_GROUP: |
| 821 | { |
| 822 | struct group_req greq; |
| 823 | struct sockaddr_in *psin; |
| 824 | struct ip_mreqn mreq; |
| 825 | |
| 826 | if (optlen < sizeof(struct group_req)) |
| 827 | goto e_inval; |
| 828 | err = -EFAULT; |
| 829 | if (copy_from_user(&greq, optval, sizeof(greq))) |
| 830 | break; |
| 831 | psin = (struct sockaddr_in *)&greq.gr_group; |
| 832 | if (psin->sin_family != AF_INET) |
| 833 | goto e_inval; |
| 834 | memset(&mreq, 0, sizeof(mreq)); |
| 835 | mreq.imr_multiaddr = psin->sin_addr; |
| 836 | mreq.imr_ifindex = greq.gr_interface; |
| 837 | |
| 838 | if (optname == MCAST_JOIN_GROUP) |
| 839 | err = ip_mc_join_group(sk, &mreq); |
| 840 | else |
| 841 | err = ip_mc_leave_group(sk, &mreq); |
| 842 | break; |
| 843 | } |
| 844 | case MCAST_JOIN_SOURCE_GROUP: |
| 845 | case MCAST_LEAVE_SOURCE_GROUP: |
| 846 | case MCAST_BLOCK_SOURCE: |
| 847 | case MCAST_UNBLOCK_SOURCE: |
| 848 | { |
| 849 | struct group_source_req greqs; |
| 850 | struct ip_mreq_source mreqs; |
| 851 | struct sockaddr_in *psin; |
| 852 | int omode, add; |
| 853 | |
| 854 | if (optlen != sizeof(struct group_source_req)) |
| 855 | goto e_inval; |
| 856 | if (copy_from_user(&greqs, optval, sizeof(greqs))) { |
| 857 | err = -EFAULT; |
| 858 | break; |
| 859 | } |
| 860 | if (greqs.gsr_group.ss_family != AF_INET || |
| 861 | greqs.gsr_source.ss_family != AF_INET) { |
| 862 | err = -EADDRNOTAVAIL; |
| 863 | break; |
| 864 | } |
| 865 | psin = (struct sockaddr_in *)&greqs.gsr_group; |
| 866 | mreqs.imr_multiaddr = psin->sin_addr.s_addr; |
| 867 | psin = (struct sockaddr_in *)&greqs.gsr_source; |
| 868 | mreqs.imr_sourceaddr = psin->sin_addr.s_addr; |
| 869 | mreqs.imr_interface = 0; /* use index for mc_source */ |
| 870 | |
| 871 | if (optname == MCAST_BLOCK_SOURCE) { |
| 872 | omode = MCAST_EXCLUDE; |
| 873 | add = 1; |
| 874 | } else if (optname == MCAST_UNBLOCK_SOURCE) { |
| 875 | omode = MCAST_EXCLUDE; |
| 876 | add = 0; |
| 877 | } else if (optname == MCAST_JOIN_SOURCE_GROUP) { |
| 878 | struct ip_mreqn mreq; |
| 879 | |
| 880 | psin = (struct sockaddr_in *)&greqs.gsr_group; |
| 881 | mreq.imr_multiaddr = psin->sin_addr; |
| 882 | mreq.imr_address.s_addr = 0; |
| 883 | mreq.imr_ifindex = greqs.gsr_interface; |
| 884 | err = ip_mc_join_group(sk, &mreq); |
| 885 | if (err && err != -EADDRINUSE) |
| 886 | break; |
| 887 | greqs.gsr_interface = mreq.imr_ifindex; |
| 888 | omode = MCAST_INCLUDE; |
| 889 | add = 1; |
| 890 | } else /* MCAST_LEAVE_SOURCE_GROUP */ { |
| 891 | omode = MCAST_INCLUDE; |
| 892 | add = 0; |
| 893 | } |
| 894 | err = ip_mc_source(add, omode, sk, &mreqs, |
| 895 | greqs.gsr_interface); |
| 896 | break; |
| 897 | } |
| 898 | case MCAST_MSFILTER: |
| 899 | { |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 900 | struct sockaddr_in *psin; |
| 901 | struct ip_msfilter *msf = NULL; |
| 902 | struct group_filter *gsf = NULL; |
| 903 | int msize, i, ifindex; |
| 904 | |
| 905 | if (optlen < GROUP_FILTER_SIZE(0)) |
| 906 | goto e_inval; |
| 907 | if (optlen > sysctl_optmem_max) { |
| 908 | err = -ENOBUFS; |
| 909 | break; |
| 910 | } |
Jianjun Kong | 09cb105 | 2008-11-03 00:27:11 -0800 | [diff] [blame] | 911 | gsf = kmalloc(optlen, GFP_KERNEL); |
Stephen Hemminger | cfcabdc | 2007-10-09 01:59:42 -0700 | [diff] [blame] | 912 | if (!gsf) { |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 913 | err = -ENOBUFS; |
| 914 | break; |
| 915 | } |
| 916 | err = -EFAULT; |
Eric Dumazet | 4d52cfb | 2009-06-02 00:42:16 -0700 | [diff] [blame] | 917 | if (copy_from_user(gsf, optval, optlen)) |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 918 | goto mc_msf_out; |
Eric Dumazet | 4d52cfb | 2009-06-02 00:42:16 -0700 | [diff] [blame] | 919 | |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 920 | /* numsrc >= (4G-140)/128 overflow in 32 bits */ |
| 921 | if (gsf->gf_numsrc >= 0x1ffffff || |
| 922 | gsf->gf_numsrc > sysctl_igmp_max_msf) { |
| 923 | err = -ENOBUFS; |
| 924 | goto mc_msf_out; |
| 925 | } |
| 926 | if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) { |
| 927 | err = -EINVAL; |
| 928 | goto mc_msf_out; |
| 929 | } |
| 930 | msize = IP_MSFILTER_SIZE(gsf->gf_numsrc); |
Jianjun Kong | 09cb105 | 2008-11-03 00:27:11 -0800 | [diff] [blame] | 931 | msf = kmalloc(msize, GFP_KERNEL); |
Stephen Hemminger | cfcabdc | 2007-10-09 01:59:42 -0700 | [diff] [blame] | 932 | if (!msf) { |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 933 | err = -ENOBUFS; |
| 934 | goto mc_msf_out; |
| 935 | } |
| 936 | ifindex = gsf->gf_interface; |
| 937 | psin = (struct sockaddr_in *)&gsf->gf_group; |
| 938 | if (psin->sin_family != AF_INET) { |
| 939 | err = -EADDRNOTAVAIL; |
| 940 | goto mc_msf_out; |
| 941 | } |
| 942 | msf->imsf_multiaddr = psin->sin_addr.s_addr; |
| 943 | msf->imsf_interface = 0; |
| 944 | msf->imsf_fmode = gsf->gf_fmode; |
| 945 | msf->imsf_numsrc = gsf->gf_numsrc; |
| 946 | err = -EADDRNOTAVAIL; |
Eric Dumazet | 4d52cfb | 2009-06-02 00:42:16 -0700 | [diff] [blame] | 947 | for (i = 0; i < gsf->gf_numsrc; ++i) { |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 948 | psin = (struct sockaddr_in *)&gsf->gf_slist[i]; |
| 949 | |
| 950 | if (psin->sin_family != AF_INET) |
| 951 | goto mc_msf_out; |
| 952 | msf->imsf_slist[i] = psin->sin_addr.s_addr; |
| 953 | } |
| 954 | kfree(gsf); |
| 955 | gsf = NULL; |
| 956 | |
| 957 | err = ip_mc_msfilter(sk, msf, ifindex); |
Eric Dumazet | 4d52cfb | 2009-06-02 00:42:16 -0700 | [diff] [blame] | 958 | mc_msf_out: |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 959 | kfree(msf); |
| 960 | kfree(gsf); |
| 961 | break; |
| 962 | } |
Nivedita Singhvi | f771bef | 2009-05-28 07:00:46 +0000 | [diff] [blame] | 963 | case IP_MULTICAST_ALL: |
| 964 | if (optlen < 1) |
| 965 | goto e_inval; |
| 966 | if (val != 0 && val != 1) |
| 967 | goto e_inval; |
| 968 | inet->mc_all = val; |
| 969 | break; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 970 | case IP_ROUTER_ALERT: |
| 971 | err = ip_ra_control(sk, val ? 1 : 0, NULL); |
| 972 | break; |
| 973 | |
| 974 | case IP_FREEBIND: |
Eric Dumazet | 4d52cfb | 2009-06-02 00:42:16 -0700 | [diff] [blame] | 975 | if (optlen < 1) |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 976 | goto e_inval; |
| 977 | inet->freebind = !!val; |
| 978 | break; |
| 979 | |
| 980 | case IP_IPSEC_POLICY: |
| 981 | case IP_XFRM_POLICY: |
| 982 | err = -EPERM; |
| 983 | if (!capable(CAP_NET_ADMIN)) |
| 984 | break; |
| 985 | err = xfrm_user_policy(sk, optname, optval, optlen); |
| 986 | break; |
| 987 | |
KOVACS Krisztian | f5715ae | 2008-10-01 07:30:02 -0700 | [diff] [blame] | 988 | case IP_TRANSPARENT: |
Maciej Żenczykowski | 6cc7a76 | 2011-10-20 18:21:36 -0400 | [diff] [blame] | 989 | if (!!val && !capable(CAP_NET_RAW) && !capable(CAP_NET_ADMIN)) { |
KOVACS Krisztian | f5715ae | 2008-10-01 07:30:02 -0700 | [diff] [blame] | 990 | err = -EPERM; |
| 991 | break; |
| 992 | } |
| 993 | if (optlen < 1) |
| 994 | goto e_inval; |
| 995 | inet->transparent = !!val; |
| 996 | break; |
| 997 | |
Stephen Hemminger | d218d11 | 2010-01-11 16:28:01 -0800 | [diff] [blame] | 998 | case IP_MINTTL: |
| 999 | if (optlen < 1) |
| 1000 | goto e_inval; |
| 1001 | if (val < 0 || val > 255) |
| 1002 | goto e_inval; |
| 1003 | inet->min_ttl = val; |
| 1004 | break; |
| 1005 | |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1006 | default: |
| 1007 | err = -ENOPROTOOPT; |
| 1008 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1009 | } |
| 1010 | release_sock(sk); |
| 1011 | return err; |
| 1012 | |
| 1013 | e_inval: |
| 1014 | release_sock(sk); |
| 1015 | return -EINVAL; |
| 1016 | } |
| 1017 | |
Eric Dumazet | f84af32 | 2010-04-28 15:31:51 -0700 | [diff] [blame] | 1018 | /** |
Eric Dumazet | d826eb1 | 2011-11-09 07:24:35 +0000 | [diff] [blame] | 1019 | * ipv4_pktinfo_prepare - transfert some info from rtable to skb |
Eric Dumazet | f84af32 | 2010-04-28 15:31:51 -0700 | [diff] [blame] | 1020 | * @sk: socket |
| 1021 | * @skb: buffer |
| 1022 | * |
David S. Miller | 35ebf65 | 2012-06-28 03:59:11 -0700 | [diff] [blame] | 1023 | * To support IP_CMSG_PKTINFO option, we store rt_iif and specific |
| 1024 | * destination in skb->cb[] before dst drop. |
Eric Dumazet | d826eb1 | 2011-11-09 07:24:35 +0000 | [diff] [blame] | 1025 | * This way, receiver doesnt make cache line misses to read rtable. |
Eric Dumazet | f84af32 | 2010-04-28 15:31:51 -0700 | [diff] [blame] | 1026 | */ |
Eric Dumazet | d826eb1 | 2011-11-09 07:24:35 +0000 | [diff] [blame] | 1027 | void ipv4_pktinfo_prepare(struct sk_buff *skb) |
Eric Dumazet | f84af32 | 2010-04-28 15:31:51 -0700 | [diff] [blame] | 1028 | { |
Eric Dumazet | d826eb1 | 2011-11-09 07:24:35 +0000 | [diff] [blame] | 1029 | struct in_pktinfo *pktinfo = PKTINFO_SKB_CB(skb); |
| 1030 | const struct rtable *rt = skb_rtable(skb); |
| 1031 | |
| 1032 | if (rt) { |
| 1033 | pktinfo->ipi_ifindex = rt->rt_iif; |
David S. Miller | 35ebf65 | 2012-06-28 03:59:11 -0700 | [diff] [blame] | 1034 | pktinfo->ipi_spec_dst.s_addr = fib_compute_spec_dst(skb); |
Eric Dumazet | d826eb1 | 2011-11-09 07:24:35 +0000 | [diff] [blame] | 1035 | } else { |
| 1036 | pktinfo->ipi_ifindex = 0; |
| 1037 | pktinfo->ipi_spec_dst.s_addr = 0; |
| 1038 | } |
| 1039 | skb_dst_drop(skb); |
Eric Dumazet | f84af32 | 2010-04-28 15:31:51 -0700 | [diff] [blame] | 1040 | } |
Eric Dumazet | f84af32 | 2010-04-28 15:31:51 -0700 | [diff] [blame] | 1041 | |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1042 | int ip_setsockopt(struct sock *sk, int level, |
David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 1043 | int optname, char __user *optval, unsigned int optlen) |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1044 | { |
| 1045 | int err; |
| 1046 | |
| 1047 | if (level != SOL_IP) |
| 1048 | return -ENOPROTOOPT; |
| 1049 | |
| 1050 | err = do_ip_setsockopt(sk, level, optname, optval, optlen); |
| 1051 | #ifdef CONFIG_NETFILTER |
| 1052 | /* we need to exclude all possible ENOPROTOOPTs except default case */ |
| 1053 | if (err == -ENOPROTOOPT && optname != IP_HDRINCL && |
Pavel Emelyanov | 6a9fb94 | 2007-11-05 21:32:31 -0800 | [diff] [blame] | 1054 | optname != IP_IPSEC_POLICY && |
| 1055 | optname != IP_XFRM_POLICY && |
| 1056 | !ip_mroute_opt(optname)) { |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1057 | lock_sock(sk); |
| 1058 | err = nf_setsockopt(sk, PF_INET, optname, optval, optlen); |
| 1059 | release_sock(sk); |
| 1060 | } |
| 1061 | #endif |
| 1062 | return err; |
| 1063 | } |
Eric Dumazet | 4d52cfb | 2009-06-02 00:42:16 -0700 | [diff] [blame] | 1064 | EXPORT_SYMBOL(ip_setsockopt); |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1065 | |
| 1066 | #ifdef CONFIG_COMPAT |
Arnaldo Carvalho de Melo | 543d9cf | 2006-03-20 22:48:35 -0800 | [diff] [blame] | 1067 | int compat_ip_setsockopt(struct sock *sk, int level, int optname, |
David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 1068 | char __user *optval, unsigned int optlen) |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1069 | { |
| 1070 | int err; |
| 1071 | |
| 1072 | if (level != SOL_IP) |
| 1073 | return -ENOPROTOOPT; |
| 1074 | |
David L Stevens | dae5029 | 2008-04-27 01:06:07 -0700 | [diff] [blame] | 1075 | if (optname >= MCAST_JOIN_GROUP && optname <= MCAST_MSFILTER) |
| 1076 | return compat_mc_setsockopt(sk, level, optname, optval, optlen, |
| 1077 | ip_setsockopt); |
| 1078 | |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1079 | err = do_ip_setsockopt(sk, level, optname, optval, optlen); |
| 1080 | #ifdef CONFIG_NETFILTER |
| 1081 | /* we need to exclude all possible ENOPROTOOPTs except default case */ |
| 1082 | if (err == -ENOPROTOOPT && optname != IP_HDRINCL && |
Pavel Emelyanov | 6a9fb94 | 2007-11-05 21:32:31 -0800 | [diff] [blame] | 1083 | optname != IP_IPSEC_POLICY && |
| 1084 | optname != IP_XFRM_POLICY && |
| 1085 | !ip_mroute_opt(optname)) { |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1086 | lock_sock(sk); |
Arnaldo Carvalho de Melo | 543d9cf | 2006-03-20 22:48:35 -0800 | [diff] [blame] | 1087 | err = compat_nf_setsockopt(sk, PF_INET, optname, |
| 1088 | optval, optlen); |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1089 | release_sock(sk); |
| 1090 | } |
| 1091 | #endif |
| 1092 | return err; |
| 1093 | } |
Arnaldo Carvalho de Melo | 543d9cf | 2006-03-20 22:48:35 -0800 | [diff] [blame] | 1094 | EXPORT_SYMBOL(compat_ip_setsockopt); |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1095 | #endif |
| 1096 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1097 | /* |
Eric Dumazet | 4d52cfb | 2009-06-02 00:42:16 -0700 | [diff] [blame] | 1098 | * Get the options. Note for future reference. The GET of IP options gets |
| 1099 | * the _received_ ones. The set sets the _sent_ ones. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1100 | */ |
| 1101 | |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1102 | static int do_ip_getsockopt(struct sock *sk, int level, int optname, |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 1103 | char __user *optval, int __user *optlen, unsigned int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1104 | { |
| 1105 | struct inet_sock *inet = inet_sk(sk); |
| 1106 | int val; |
| 1107 | int len; |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1108 | |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1109 | if (level != SOL_IP) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1110 | return -EOPNOTSUPP; |
| 1111 | |
Pavel Emelyanov | 6a9fb94 | 2007-11-05 21:32:31 -0800 | [diff] [blame] | 1112 | if (ip_mroute_opt(optname)) |
Jianjun Kong | 09cb105 | 2008-11-03 00:27:11 -0800 | [diff] [blame] | 1113 | return ip_mroute_getsockopt(sk, optname, optval, optlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1114 | |
Jianjun Kong | 09cb105 | 2008-11-03 00:27:11 -0800 | [diff] [blame] | 1115 | if (get_user(len, optlen)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1116 | return -EFAULT; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1117 | if (len < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1118 | return -EINVAL; |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1119 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1120 | lock_sock(sk); |
| 1121 | |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1122 | switch (optname) { |
| 1123 | case IP_OPTIONS: |
| 1124 | { |
| 1125 | unsigned char optbuf[sizeof(struct ip_options)+40]; |
Eric Dumazet | f6d8bd0 | 2011-04-21 09:45:37 +0000 | [diff] [blame] | 1126 | struct ip_options *opt = (struct ip_options *)optbuf; |
| 1127 | struct ip_options_rcu *inet_opt; |
| 1128 | |
| 1129 | inet_opt = rcu_dereference_protected(inet->inet_opt, |
| 1130 | sock_owned_by_user(sk)); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1131 | opt->optlen = 0; |
Eric Dumazet | f6d8bd0 | 2011-04-21 09:45:37 +0000 | [diff] [blame] | 1132 | if (inet_opt) |
| 1133 | memcpy(optbuf, &inet_opt->opt, |
| 1134 | sizeof(struct ip_options) + |
| 1135 | inet_opt->opt.optlen); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1136 | release_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1137 | |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1138 | if (opt->optlen == 0) |
| 1139 | return put_user(0, optlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1140 | |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1141 | ip_options_undo(opt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1142 | |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1143 | len = min_t(unsigned int, len, opt->optlen); |
| 1144 | if (put_user(len, optlen)) |
| 1145 | return -EFAULT; |
| 1146 | if (copy_to_user(optval, opt->__data, len)) |
| 1147 | return -EFAULT; |
| 1148 | return 0; |
| 1149 | } |
| 1150 | case IP_PKTINFO: |
| 1151 | val = (inet->cmsg_flags & IP_CMSG_PKTINFO) != 0; |
| 1152 | break; |
| 1153 | case IP_RECVTTL: |
| 1154 | val = (inet->cmsg_flags & IP_CMSG_TTL) != 0; |
| 1155 | break; |
| 1156 | case IP_RECVTOS: |
| 1157 | val = (inet->cmsg_flags & IP_CMSG_TOS) != 0; |
| 1158 | break; |
| 1159 | case IP_RECVOPTS: |
| 1160 | val = (inet->cmsg_flags & IP_CMSG_RECVOPTS) != 0; |
| 1161 | break; |
| 1162 | case IP_RETOPTS: |
| 1163 | val = (inet->cmsg_flags & IP_CMSG_RETOPTS) != 0; |
| 1164 | break; |
| 1165 | case IP_PASSSEC: |
| 1166 | val = (inet->cmsg_flags & IP_CMSG_PASSSEC) != 0; |
| 1167 | break; |
Balazs Scheidler | e8b2dfe | 2008-11-16 19:32:39 -0800 | [diff] [blame] | 1168 | case IP_RECVORIGDSTADDR: |
| 1169 | val = (inet->cmsg_flags & IP_CMSG_ORIGDSTADDR) != 0; |
| 1170 | break; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1171 | case IP_TOS: |
| 1172 | val = inet->tos; |
| 1173 | break; |
| 1174 | case IP_TTL: |
| 1175 | val = (inet->uc_ttl == -1 ? |
| 1176 | sysctl_ip_default_ttl : |
| 1177 | inet->uc_ttl); |
| 1178 | break; |
| 1179 | case IP_HDRINCL: |
| 1180 | val = inet->hdrincl; |
| 1181 | break; |
Michael Kerrisk | a89b476 | 2010-09-10 20:26:56 +0000 | [diff] [blame] | 1182 | case IP_NODEFRAG: |
| 1183 | val = inet->nodefrag; |
| 1184 | break; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1185 | case IP_MTU_DISCOVER: |
| 1186 | val = inet->pmtudisc; |
| 1187 | break; |
| 1188 | case IP_MTU: |
| 1189 | { |
| 1190 | struct dst_entry *dst; |
| 1191 | val = 0; |
| 1192 | dst = sk_dst_get(sk); |
| 1193 | if (dst) { |
| 1194 | val = dst_mtu(dst); |
| 1195 | dst_release(dst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1196 | } |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1197 | if (!val) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1198 | release_sock(sk); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1199 | return -ENOTCONN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1200 | } |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1201 | break; |
| 1202 | } |
| 1203 | case IP_RECVERR: |
| 1204 | val = inet->recverr; |
| 1205 | break; |
| 1206 | case IP_MULTICAST_TTL: |
| 1207 | val = inet->mc_ttl; |
| 1208 | break; |
| 1209 | case IP_MULTICAST_LOOP: |
| 1210 | val = inet->mc_loop; |
| 1211 | break; |
Erich E. Hoover | 76e2105 | 2012-02-08 09:11:07 +0000 | [diff] [blame] | 1212 | case IP_UNICAST_IF: |
| 1213 | val = (__force int)htonl((__u32) inet->uc_index); |
| 1214 | break; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1215 | case IP_MULTICAST_IF: |
| 1216 | { |
| 1217 | struct in_addr addr; |
| 1218 | len = min_t(unsigned int, len, sizeof(struct in_addr)); |
| 1219 | addr.s_addr = inet->mc_addr; |
| 1220 | release_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1221 | |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1222 | if (put_user(len, optlen)) |
| 1223 | return -EFAULT; |
| 1224 | if (copy_to_user(optval, &addr, len)) |
| 1225 | return -EFAULT; |
| 1226 | return 0; |
| 1227 | } |
| 1228 | case IP_MSFILTER: |
| 1229 | { |
| 1230 | struct ip_msfilter msf; |
| 1231 | int err; |
| 1232 | |
| 1233 | if (len < IP_MSFILTER_SIZE(0)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1234 | release_sock(sk); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1235 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1236 | } |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1237 | if (copy_from_user(&msf, optval, IP_MSFILTER_SIZE(0))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1238 | release_sock(sk); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1239 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1240 | } |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1241 | err = ip_mc_msfget(sk, &msf, |
| 1242 | (struct ip_msfilter __user *)optval, optlen); |
| 1243 | release_sock(sk); |
| 1244 | return err; |
| 1245 | } |
| 1246 | case MCAST_MSFILTER: |
| 1247 | { |
| 1248 | struct group_filter gsf; |
| 1249 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1250 | |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1251 | if (len < GROUP_FILTER_SIZE(0)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1252 | release_sock(sk); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1253 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1254 | } |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1255 | if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1256 | release_sock(sk); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1257 | return -EFAULT; |
| 1258 | } |
| 1259 | err = ip_mc_gsfget(sk, &gsf, |
Eric Dumazet | 4d52cfb | 2009-06-02 00:42:16 -0700 | [diff] [blame] | 1260 | (struct group_filter __user *)optval, |
| 1261 | optlen); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1262 | release_sock(sk); |
| 1263 | return err; |
| 1264 | } |
Nivedita Singhvi | f771bef | 2009-05-28 07:00:46 +0000 | [diff] [blame] | 1265 | case IP_MULTICAST_ALL: |
| 1266 | val = inet->mc_all; |
| 1267 | break; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1268 | case IP_PKTOPTIONS: |
| 1269 | { |
| 1270 | struct msghdr msg; |
| 1271 | |
| 1272 | release_sock(sk); |
| 1273 | |
| 1274 | if (sk->sk_type != SOCK_STREAM) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1275 | return -ENOPROTOOPT; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1276 | |
| 1277 | msg.msg_control = optval; |
| 1278 | msg.msg_controllen = len; |
Daniel Baluta | dd23198e | 2011-08-07 22:31:07 -0700 | [diff] [blame] | 1279 | msg.msg_flags = flags; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1280 | |
| 1281 | if (inet->cmsg_flags & IP_CMSG_PKTINFO) { |
| 1282 | struct in_pktinfo info; |
| 1283 | |
Eric Dumazet | c720c7e | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 1284 | info.ipi_addr.s_addr = inet->inet_rcv_saddr; |
| 1285 | info.ipi_spec_dst.s_addr = inet->inet_rcv_saddr; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1286 | info.ipi_ifindex = inet->mc_index; |
| 1287 | put_cmsg(&msg, SOL_IP, IP_PKTINFO, sizeof(info), &info); |
| 1288 | } |
| 1289 | if (inet->cmsg_flags & IP_CMSG_TTL) { |
| 1290 | int hlim = inet->mc_ttl; |
| 1291 | put_cmsg(&msg, SOL_IP, IP_TTL, sizeof(hlim), &hlim); |
| 1292 | } |
Jiri Benc | 4c507d2 | 2012-02-09 09:35:49 +0000 | [diff] [blame] | 1293 | if (inet->cmsg_flags & IP_CMSG_TOS) { |
| 1294 | int tos = inet->rcv_tos; |
| 1295 | put_cmsg(&msg, SOL_IP, IP_TOS, sizeof(tos), &tos); |
| 1296 | } |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1297 | len -= msg.msg_controllen; |
| 1298 | return put_user(len, optlen); |
| 1299 | } |
| 1300 | case IP_FREEBIND: |
| 1301 | val = inet->freebind; |
| 1302 | break; |
KOVACS Krisztian | f5715ae | 2008-10-01 07:30:02 -0700 | [diff] [blame] | 1303 | case IP_TRANSPARENT: |
| 1304 | val = inet->transparent; |
| 1305 | break; |
Stephen Hemminger | d218d11 | 2010-01-11 16:28:01 -0800 | [diff] [blame] | 1306 | case IP_MINTTL: |
| 1307 | val = inet->min_ttl; |
| 1308 | break; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1309 | default: |
| 1310 | release_sock(sk); |
| 1311 | return -ENOPROTOOPT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1312 | } |
| 1313 | release_sock(sk); |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1314 | |
Eric Dumazet | 4d52cfb | 2009-06-02 00:42:16 -0700 | [diff] [blame] | 1315 | if (len < sizeof(int) && len > 0 && val >= 0 && val <= 255) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1316 | unsigned char ucval = (unsigned char)val; |
| 1317 | len = 1; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1318 | if (put_user(len, optlen)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1319 | return -EFAULT; |
Jianjun Kong | 09cb105 | 2008-11-03 00:27:11 -0800 | [diff] [blame] | 1320 | if (copy_to_user(optval, &ucval, 1)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1321 | return -EFAULT; |
| 1322 | } else { |
| 1323 | len = min_t(unsigned int, sizeof(int), len); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1324 | if (put_user(len, optlen)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1325 | return -EFAULT; |
Jianjun Kong | 09cb105 | 2008-11-03 00:27:11 -0800 | [diff] [blame] | 1326 | if (copy_to_user(optval, &val, len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1327 | return -EFAULT; |
| 1328 | } |
| 1329 | return 0; |
| 1330 | } |
| 1331 | |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1332 | int ip_getsockopt(struct sock *sk, int level, |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1333 | int optname, char __user *optval, int __user *optlen) |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1334 | { |
| 1335 | int err; |
| 1336 | |
Daniel Baluta | dd23198e | 2011-08-07 22:31:07 -0700 | [diff] [blame] | 1337 | err = do_ip_getsockopt(sk, level, optname, optval, optlen, 0); |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1338 | #ifdef CONFIG_NETFILTER |
| 1339 | /* we need to exclude all possible ENOPROTOOPTs except default case */ |
Pavel Emelyanov | 6a9fb94 | 2007-11-05 21:32:31 -0800 | [diff] [blame] | 1340 | if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS && |
| 1341 | !ip_mroute_opt(optname)) { |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1342 | int len; |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1343 | |
Jianjun Kong | 09cb105 | 2008-11-03 00:27:11 -0800 | [diff] [blame] | 1344 | if (get_user(len, optlen)) |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1345 | return -EFAULT; |
| 1346 | |
| 1347 | lock_sock(sk); |
| 1348 | err = nf_getsockopt(sk, PF_INET, optname, optval, |
| 1349 | &len); |
| 1350 | release_sock(sk); |
| 1351 | if (err >= 0) |
| 1352 | err = put_user(len, optlen); |
| 1353 | return err; |
| 1354 | } |
| 1355 | #endif |
| 1356 | return err; |
| 1357 | } |
Eric Dumazet | 4d52cfb | 2009-06-02 00:42:16 -0700 | [diff] [blame] | 1358 | EXPORT_SYMBOL(ip_getsockopt); |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1359 | |
| 1360 | #ifdef CONFIG_COMPAT |
Arnaldo Carvalho de Melo | 543d9cf | 2006-03-20 22:48:35 -0800 | [diff] [blame] | 1361 | int compat_ip_getsockopt(struct sock *sk, int level, int optname, |
| 1362 | char __user *optval, int __user *optlen) |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1363 | { |
David L Stevens | 42908c6 | 2008-04-29 03:23:22 -0700 | [diff] [blame] | 1364 | int err; |
| 1365 | |
| 1366 | if (optname == MCAST_MSFILTER) |
| 1367 | return compat_mc_getsockopt(sk, level, optname, optval, optlen, |
| 1368 | ip_getsockopt); |
| 1369 | |
Daniel Baluta | dd23198e | 2011-08-07 22:31:07 -0700 | [diff] [blame] | 1370 | err = do_ip_getsockopt(sk, level, optname, optval, optlen, |
| 1371 | MSG_CMSG_COMPAT); |
David L Stevens | 42908c6 | 2008-04-29 03:23:22 -0700 | [diff] [blame] | 1372 | |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1373 | #ifdef CONFIG_NETFILTER |
| 1374 | /* we need to exclude all possible ENOPROTOOPTs except default case */ |
Pavel Emelyanov | 6a9fb94 | 2007-11-05 21:32:31 -0800 | [diff] [blame] | 1375 | if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS && |
| 1376 | !ip_mroute_opt(optname)) { |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1377 | int len; |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1378 | |
Arnaldo Carvalho de Melo | 543d9cf | 2006-03-20 22:48:35 -0800 | [diff] [blame] | 1379 | if (get_user(len, optlen)) |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1380 | return -EFAULT; |
| 1381 | |
| 1382 | lock_sock(sk); |
Arnaldo Carvalho de Melo | 543d9cf | 2006-03-20 22:48:35 -0800 | [diff] [blame] | 1383 | err = compat_nf_getsockopt(sk, PF_INET, optname, optval, &len); |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1384 | release_sock(sk); |
| 1385 | if (err >= 0) |
| 1386 | err = put_user(len, optlen); |
| 1387 | return err; |
| 1388 | } |
| 1389 | #endif |
| 1390 | return err; |
| 1391 | } |
Arnaldo Carvalho de Melo | 543d9cf | 2006-03-20 22:48:35 -0800 | [diff] [blame] | 1392 | EXPORT_SYMBOL(compat_ip_getsockopt); |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1393 | #endif |