diff options
Diffstat (limited to 'net/ipv6/sit.c')
-rw-r--r-- | net/ipv6/sit.c | 257 |
1 files changed, 213 insertions, 44 deletions
diff --git a/net/ipv6/sit.c b/net/ipv6/sit.c index 1b8196c8d145..cc16fe07bbff 100644 --- a/net/ipv6/sit.c +++ b/net/ipv6/sit.c | |||
@@ -16,7 +16,7 @@ | |||
16 | * Changes: | 16 | * Changes: |
17 | * Roger Venning <r.venning@telstra.com>: 6to4 support | 17 | * Roger Venning <r.venning@telstra.com>: 6to4 support |
18 | * Nate Thompson <nate@thebog.net>: 6to4 support | 18 | * Nate Thompson <nate@thebog.net>: 6to4 support |
19 | * Fred L. Templin <fltemplin@acm.org>: isatap support | 19 | * Fred Templin <fred.l.templin@boeing.com>: isatap support |
20 | */ | 20 | */ |
21 | 21 | ||
22 | #include <linux/module.h> | 22 | #include <linux/module.h> |
@@ -197,6 +197,179 @@ failed: | |||
197 | return NULL; | 197 | return NULL; |
198 | } | 198 | } |
199 | 199 | ||
200 | static struct ip_tunnel_prl_entry * | ||
201 | __ipip6_tunnel_locate_prl(struct ip_tunnel *t, __be32 addr) | ||
202 | { | ||
203 | struct ip_tunnel_prl_entry *p = (struct ip_tunnel_prl_entry *)NULL; | ||
204 | |||
205 | for (p = t->prl; p; p = p->next) | ||
206 | if (p->addr == addr) | ||
207 | break; | ||
208 | return p; | ||
209 | |||
210 | } | ||
211 | |||
212 | static int ipip6_tunnel_get_prl(struct ip_tunnel *t, struct ip_tunnel_prl *a) | ||
213 | { | ||
214 | struct ip_tunnel_prl *kp; | ||
215 | struct ip_tunnel_prl_entry *prl; | ||
216 | unsigned int cmax, c = 0, ca, len; | ||
217 | int ret = 0; | ||
218 | |||
219 | cmax = a->datalen / sizeof(*a); | ||
220 | if (cmax > 1 && a->addr != htonl(INADDR_ANY)) | ||
221 | cmax = 1; | ||
222 | |||
223 | /* For simple GET or for root users, | ||
224 | * we try harder to allocate. | ||
225 | */ | ||
226 | kp = (cmax <= 1 || capable(CAP_NET_ADMIN)) ? | ||
227 | kcalloc(cmax, sizeof(*kp), GFP_KERNEL) : | ||
228 | NULL; | ||
229 | |||
230 | read_lock(&ipip6_lock); | ||
231 | |||
232 | ca = t->prl_count < cmax ? t->prl_count : cmax; | ||
233 | |||
234 | if (!kp) { | ||
235 | /* We don't try hard to allocate much memory for | ||
236 | * non-root users. | ||
237 | * For root users, retry allocating enough memory for | ||
238 | * the answer. | ||
239 | */ | ||
240 | kp = kcalloc(ca, sizeof(*kp), GFP_ATOMIC); | ||
241 | if (!kp) { | ||
242 | ret = -ENOMEM; | ||
243 | goto out; | ||
244 | } | ||
245 | } | ||
246 | |||
247 | c = 0; | ||
248 | for (prl = t->prl; prl; prl = prl->next) { | ||
249 | if (c > cmax) | ||
250 | break; | ||
251 | if (a->addr != htonl(INADDR_ANY) && prl->addr != a->addr) | ||
252 | continue; | ||
253 | kp[c].addr = prl->addr; | ||
254 | kp[c].flags = prl->flags; | ||
255 | c++; | ||
256 | if (a->addr != htonl(INADDR_ANY)) | ||
257 | break; | ||
258 | } | ||
259 | out: | ||
260 | read_unlock(&ipip6_lock); | ||
261 | |||
262 | len = sizeof(*kp) * c; | ||
263 | ret = len ? copy_to_user(a->data, kp, len) : 0; | ||
264 | |||
265 | kfree(kp); | ||
266 | if (ret) | ||
267 | return -EFAULT; | ||
268 | |||
269 | a->datalen = len; | ||
270 | return 0; | ||
271 | } | ||
272 | |||
273 | static int | ||
274 | ipip6_tunnel_add_prl(struct ip_tunnel *t, struct ip_tunnel_prl *a, int chg) | ||
275 | { | ||
276 | struct ip_tunnel_prl_entry *p; | ||
277 | int err = 0; | ||
278 | |||
279 | if (a->addr == htonl(INADDR_ANY)) | ||
280 | return -EINVAL; | ||
281 | |||
282 | write_lock(&ipip6_lock); | ||
283 | |||
284 | for (p = t->prl; p; p = p->next) { | ||
285 | if (p->addr == a->addr) { | ||
286 | if (chg) | ||
287 | goto update; | ||
288 | err = -EEXIST; | ||
289 | goto out; | ||
290 | } | ||
291 | } | ||
292 | |||
293 | if (chg) { | ||
294 | err = -ENXIO; | ||
295 | goto out; | ||
296 | } | ||
297 | |||
298 | p = kzalloc(sizeof(struct ip_tunnel_prl_entry), GFP_KERNEL); | ||
299 | if (!p) { | ||
300 | err = -ENOBUFS; | ||
301 | goto out; | ||
302 | } | ||
303 | |||
304 | p->next = t->prl; | ||
305 | t->prl = p; | ||
306 | t->prl_count++; | ||
307 | update: | ||
308 | p->addr = a->addr; | ||
309 | p->flags = a->flags; | ||
310 | out: | ||
311 | write_unlock(&ipip6_lock); | ||
312 | return err; | ||
313 | } | ||
314 | |||
315 | static int | ||
316 | ipip6_tunnel_del_prl(struct ip_tunnel *t, struct ip_tunnel_prl *a) | ||
317 | { | ||
318 | struct ip_tunnel_prl_entry *x, **p; | ||
319 | int err = 0; | ||
320 | |||
321 | write_lock(&ipip6_lock); | ||
322 | |||
323 | if (a && a->addr != htonl(INADDR_ANY)) { | ||
324 | for (p = &t->prl; *p; p = &(*p)->next) { | ||
325 | if ((*p)->addr == a->addr) { | ||
326 | x = *p; | ||
327 | *p = x->next; | ||
328 | kfree(x); | ||
329 | t->prl_count--; | ||
330 | goto out; | ||
331 | } | ||
332 | } | ||
333 | err = -ENXIO; | ||
334 | } else { | ||
335 | while (t->prl) { | ||
336 | x = t->prl; | ||
337 | t->prl = t->prl->next; | ||
338 | kfree(x); | ||
339 | t->prl_count--; | ||
340 | } | ||
341 | } | ||
342 | out: | ||
343 | write_unlock(&ipip6_lock); | ||
344 | return 0; | ||
345 | } | ||
346 | |||
347 | static int | ||
348 | isatap_chksrc(struct sk_buff *skb, struct iphdr *iph, struct ip_tunnel *t) | ||
349 | { | ||
350 | struct ip_tunnel_prl_entry *p; | ||
351 | int ok = 1; | ||
352 | |||
353 | read_lock(&ipip6_lock); | ||
354 | p = __ipip6_tunnel_locate_prl(t, iph->saddr); | ||
355 | if (p) { | ||
356 | if (p->flags & PRL_DEFAULT) | ||
357 | skb->ndisc_nodetype = NDISC_NODETYPE_DEFAULT; | ||
358 | else | ||
359 | skb->ndisc_nodetype = NDISC_NODETYPE_NODEFAULT; | ||
360 | } else { | ||
361 | struct in6_addr *addr6 = &ipv6_hdr(skb)->saddr; | ||
362 | if (ipv6_addr_is_isatap(addr6) && | ||
363 | (addr6->s6_addr32[3] == iph->saddr) && | ||
364 | ipv6_chk_prefix(addr6, t->dev)) | ||
365 | skb->ndisc_nodetype = NDISC_NODETYPE_HOST; | ||
366 | else | ||
367 | ok = 0; | ||
368 | } | ||
369 | read_unlock(&ipip6_lock); | ||
370 | return ok; | ||
371 | } | ||
372 | |||
200 | static void ipip6_tunnel_uninit(struct net_device *dev) | 373 | static void ipip6_tunnel_uninit(struct net_device *dev) |
201 | { | 374 | { |
202 | if (dev == ipip6_fb_tunnel_dev) { | 375 | if (dev == ipip6_fb_tunnel_dev) { |
@@ -206,6 +379,7 @@ static void ipip6_tunnel_uninit(struct net_device *dev) | |||
206 | dev_put(dev); | 379 | dev_put(dev); |
207 | } else { | 380 | } else { |
208 | ipip6_tunnel_unlink(netdev_priv(dev)); | 381 | ipip6_tunnel_unlink(netdev_priv(dev)); |
382 | ipip6_tunnel_del_prl(netdev_priv(dev), 0); | ||
209 | dev_put(dev); | 383 | dev_put(dev); |
210 | } | 384 | } |
211 | } | 385 | } |
@@ -365,48 +539,6 @@ static inline void ipip6_ecn_decapsulate(struct iphdr *iph, struct sk_buff *skb) | |||
365 | IP6_ECN_set_ce(ipv6_hdr(skb)); | 539 | IP6_ECN_set_ce(ipv6_hdr(skb)); |
366 | } | 540 | } |
367 | 541 | ||
368 | /* ISATAP (RFC4214) - check source address */ | ||
369 | static int | ||
370 | isatap_srcok(struct sk_buff *skb, struct iphdr *iph, struct net_device *dev) | ||
371 | { | ||
372 | struct neighbour *neigh; | ||
373 | struct dst_entry *dst; | ||
374 | struct rt6_info *rt; | ||
375 | struct flowi fl; | ||
376 | struct in6_addr *addr6; | ||
377 | struct in6_addr rtr; | ||
378 | struct ipv6hdr *iph6; | ||
379 | int ok = 0; | ||
380 | |||
381 | /* from onlink default router */ | ||
382 | ipv6_addr_set(&rtr, htonl(0xFE800000), 0, 0, 0); | ||
383 | ipv6_isatap_eui64(rtr.s6_addr + 8, iph->saddr); | ||
384 | if ((rt = rt6_get_dflt_router(&rtr, dev))) { | ||
385 | dst_release(&rt->u.dst); | ||
386 | return 1; | ||
387 | } | ||
388 | |||
389 | iph6 = ipv6_hdr(skb); | ||
390 | memset(&fl, 0, sizeof(fl)); | ||
391 | fl.proto = iph6->nexthdr; | ||
392 | ipv6_addr_copy(&fl.fl6_dst, &iph6->saddr); | ||
393 | fl.oif = dev->ifindex; | ||
394 | security_skb_classify_flow(skb, &fl); | ||
395 | |||
396 | dst = ip6_route_output(&init_net, NULL, &fl); | ||
397 | if (!dst->error && (dst->dev == dev) && (neigh = dst->neighbour)) { | ||
398 | |||
399 | addr6 = (struct in6_addr*)&neigh->primary_key; | ||
400 | |||
401 | /* from correct previous hop */ | ||
402 | if (ipv6_addr_is_isatap(addr6) && | ||
403 | (addr6->s6_addr32[3] == iph->saddr)) | ||
404 | ok = 1; | ||
405 | } | ||
406 | dst_release(dst); | ||
407 | return ok; | ||
408 | } | ||
409 | |||
410 | static int ipip6_rcv(struct sk_buff *skb) | 542 | static int ipip6_rcv(struct sk_buff *skb) |
411 | { | 543 | { |
412 | struct iphdr *iph; | 544 | struct iphdr *iph; |
@@ -427,7 +559,7 @@ static int ipip6_rcv(struct sk_buff *skb) | |||
427 | skb->pkt_type = PACKET_HOST; | 559 | skb->pkt_type = PACKET_HOST; |
428 | 560 | ||
429 | if ((tunnel->dev->priv_flags & IFF_ISATAP) && | 561 | if ((tunnel->dev->priv_flags & IFF_ISATAP) && |
430 | !isatap_srcok(skb, iph, tunnel->dev)) { | 562 | !isatap_chksrc(skb, iph, tunnel)) { |
431 | tunnel->stat.rx_errors++; | 563 | tunnel->stat.rx_errors++; |
432 | read_unlock(&ipip6_lock); | 564 | read_unlock(&ipip6_lock); |
433 | kfree_skb(skb); | 565 | kfree_skb(skb); |
@@ -707,6 +839,7 @@ ipip6_tunnel_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd) | |||
707 | { | 839 | { |
708 | int err = 0; | 840 | int err = 0; |
709 | struct ip_tunnel_parm p; | 841 | struct ip_tunnel_parm p; |
842 | struct ip_tunnel_prl prl; | ||
710 | struct ip_tunnel *t; | 843 | struct ip_tunnel *t; |
711 | 844 | ||
712 | switch (cmd) { | 845 | switch (cmd) { |
@@ -806,6 +939,42 @@ ipip6_tunnel_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd) | |||
806 | err = 0; | 939 | err = 0; |
807 | break; | 940 | break; |
808 | 941 | ||
942 | case SIOCGETPRL: | ||
943 | case SIOCADDPRL: | ||
944 | case SIOCDELPRL: | ||
945 | case SIOCCHGPRL: | ||
946 | err = -EPERM; | ||
947 | if (cmd != SIOCGETPRL && !capable(CAP_NET_ADMIN)) | ||
948 | goto done; | ||
949 | err = -EINVAL; | ||
950 | if (dev == ipip6_fb_tunnel_dev) | ||
951 | goto done; | ||
952 | err = -EFAULT; | ||
953 | if (copy_from_user(&prl, ifr->ifr_ifru.ifru_data, sizeof(prl))) | ||
954 | goto done; | ||
955 | err = -ENOENT; | ||
956 | if (!(t = netdev_priv(dev))) | ||
957 | goto done; | ||
958 | |||
959 | switch (cmd) { | ||
960 | case SIOCGETPRL: | ||
961 | err = ipip6_tunnel_get_prl(t, &prl); | ||
962 | if (!err && copy_to_user(ifr->ifr_ifru.ifru_data, | ||
963 | &prl, sizeof(prl))) | ||
964 | err = -EFAULT; | ||
965 | break; | ||
966 | case SIOCDELPRL: | ||
967 | err = ipip6_tunnel_del_prl(t, &prl); | ||
968 | break; | ||
969 | case SIOCADDPRL: | ||
970 | case SIOCCHGPRL: | ||
971 | err = ipip6_tunnel_add_prl(t, &prl, cmd == SIOCCHGPRL); | ||
972 | break; | ||
973 | } | ||
974 | if (cmd != SIOCGETPRL) | ||
975 | netdev_state_change(dev); | ||
976 | break; | ||
977 | |||
809 | default: | 978 | default: |
810 | err = -EINVAL; | 979 | err = -EINVAL; |
811 | } | 980 | } |