PageRenderTime 55ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/net/ipv6/ip6_output.c

https://github.com/mstsirkin/kvm
C | 1645 lines | 1162 code | 226 blank | 257 comment | 247 complexity | bc62c4d306a1c9187fd391d5df9b8e07 MD5 | raw file
  1. /*
  2. * IPv6 output functions
  3. * Linux INET6 implementation
  4. *
  5. * Authors:
  6. * Pedro Roque <roque@di.fc.ul.pt>
  7. *
  8. * Based on linux/net/ipv4/ip_output.c
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version
  13. * 2 of the License, or (at your option) any later version.
  14. *
  15. * Changes:
  16. * A.N.Kuznetsov : airthmetics in fragmentation.
  17. * extension headers are implemented.
  18. * route changes now work.
  19. * ip6_forward does not confuse sniffers.
  20. * etc.
  21. *
  22. * H. von Brand : Added missing #include <linux/string.h>
  23. * Imran Patel : frag id should be in NBO
  24. * Kazunori MIYAZAWA @USAGI
  25. * : add ip6_append_data and related functions
  26. * for datagram xmit
  27. */
  28. #include <linux/errno.h>
  29. #include <linux/kernel.h>
  30. #include <linux/string.h>
  31. #include <linux/socket.h>
  32. #include <linux/net.h>
  33. #include <linux/netdevice.h>
  34. #include <linux/if_arp.h>
  35. #include <linux/in6.h>
  36. #include <linux/tcp.h>
  37. #include <linux/route.h>
  38. #include <linux/module.h>
  39. #include <linux/slab.h>
  40. #include <linux/netfilter.h>
  41. #include <linux/netfilter_ipv6.h>
  42. #include <net/sock.h>
  43. #include <net/snmp.h>
  44. #include <net/ipv6.h>
  45. #include <net/ndisc.h>
  46. #include <net/protocol.h>
  47. #include <net/ip6_route.h>
  48. #include <net/addrconf.h>
  49. #include <net/rawv6.h>
  50. #include <net/icmp.h>
  51. #include <net/xfrm.h>
  52. #include <net/checksum.h>
  53. #include <linux/mroute6.h>
  54. int ip6_fragment(struct sk_buff *skb, int (*output)(struct sk_buff *));
  55. int __ip6_local_out(struct sk_buff *skb)
  56. {
  57. int len;
  58. len = skb->len - sizeof(struct ipv6hdr);
  59. if (len > IPV6_MAXPLEN)
  60. len = 0;
  61. ipv6_hdr(skb)->payload_len = htons(len);
  62. return nf_hook(NFPROTO_IPV6, NF_INET_LOCAL_OUT, skb, NULL,
  63. skb_dst(skb)->dev, dst_output);
  64. }
  65. int ip6_local_out(struct sk_buff *skb)
  66. {
  67. int err;
  68. err = __ip6_local_out(skb);
  69. if (likely(err == 1))
  70. err = dst_output(skb);
  71. return err;
  72. }
  73. EXPORT_SYMBOL_GPL(ip6_local_out);
  74. /* dev_loopback_xmit for use with netfilter. */
  75. static int ip6_dev_loopback_xmit(struct sk_buff *newskb)
  76. {
  77. skb_reset_mac_header(newskb);
  78. __skb_pull(newskb, skb_network_offset(newskb));
  79. newskb->pkt_type = PACKET_LOOPBACK;
  80. newskb->ip_summed = CHECKSUM_UNNECESSARY;
  81. WARN_ON(!skb_dst(newskb));
  82. netif_rx_ni(newskb);
  83. return 0;
  84. }
  85. static int ip6_finish_output2(struct sk_buff *skb)
  86. {
  87. struct dst_entry *dst = skb_dst(skb);
  88. struct net_device *dev = dst->dev;
  89. struct neighbour *neigh;
  90. skb->protocol = htons(ETH_P_IPV6);
  91. skb->dev = dev;
  92. if (ipv6_addr_is_multicast(&ipv6_hdr(skb)->daddr)) {
  93. struct inet6_dev *idev = ip6_dst_idev(skb_dst(skb));
  94. if (!(dev->flags & IFF_LOOPBACK) && sk_mc_loop(skb->sk) &&
  95. ((mroute6_socket(dev_net(dev), skb) &&
  96. !(IP6CB(skb)->flags & IP6SKB_FORWARDED)) ||
  97. ipv6_chk_mcast_addr(dev, &ipv6_hdr(skb)->daddr,
  98. &ipv6_hdr(skb)->saddr))) {
  99. struct sk_buff *newskb = skb_clone(skb, GFP_ATOMIC);
  100. /* Do not check for IFF_ALLMULTI; multicast routing
  101. is not supported in any case.
  102. */
  103. if (newskb)
  104. NF_HOOK(NFPROTO_IPV6, NF_INET_POST_ROUTING,
  105. newskb, NULL, newskb->dev,
  106. ip6_dev_loopback_xmit);
  107. if (ipv6_hdr(skb)->hop_limit == 0) {
  108. IP6_INC_STATS(dev_net(dev), idev,
  109. IPSTATS_MIB_OUTDISCARDS);
  110. kfree_skb(skb);
  111. return 0;
  112. }
  113. }
  114. IP6_UPD_PO_STATS(dev_net(dev), idev, IPSTATS_MIB_OUTMCAST,
  115. skb->len);
  116. }
  117. rcu_read_lock();
  118. neigh = dst_get_neighbour(dst);
  119. if (neigh) {
  120. int res = neigh_output(neigh, skb);
  121. rcu_read_unlock();
  122. return res;
  123. }
  124. rcu_read_unlock();
  125. IP6_INC_STATS_BH(dev_net(dst->dev),
  126. ip6_dst_idev(dst), IPSTATS_MIB_OUTNOROUTES);
  127. kfree_skb(skb);
  128. return -EINVAL;
  129. }
  130. static int ip6_finish_output(struct sk_buff *skb)
  131. {
  132. if ((skb->len > ip6_skb_dst_mtu(skb) && !skb_is_gso(skb)) ||
  133. dst_allfrag(skb_dst(skb)))
  134. return ip6_fragment(skb, ip6_finish_output2);
  135. else
  136. return ip6_finish_output2(skb);
  137. }
  138. int ip6_output(struct sk_buff *skb)
  139. {
  140. struct net_device *dev = skb_dst(skb)->dev;
  141. struct inet6_dev *idev = ip6_dst_idev(skb_dst(skb));
  142. if (unlikely(idev->cnf.disable_ipv6)) {
  143. IP6_INC_STATS(dev_net(dev), idev,
  144. IPSTATS_MIB_OUTDISCARDS);
  145. kfree_skb(skb);
  146. return 0;
  147. }
  148. return NF_HOOK_COND(NFPROTO_IPV6, NF_INET_POST_ROUTING, skb, NULL, dev,
  149. ip6_finish_output,
  150. !(IP6CB(skb)->flags & IP6SKB_REROUTED));
  151. }
  152. /*
  153. * xmit an sk_buff (used by TCP, SCTP and DCCP)
  154. */
  155. int ip6_xmit(struct sock *sk, struct sk_buff *skb, struct flowi6 *fl6,
  156. struct ipv6_txoptions *opt)
  157. {
  158. struct net *net = sock_net(sk);
  159. struct ipv6_pinfo *np = inet6_sk(sk);
  160. struct in6_addr *first_hop = &fl6->daddr;
  161. struct dst_entry *dst = skb_dst(skb);
  162. struct ipv6hdr *hdr;
  163. u8 proto = fl6->flowi6_proto;
  164. int seg_len = skb->len;
  165. int hlimit = -1;
  166. int tclass = 0;
  167. u32 mtu;
  168. if (opt) {
  169. unsigned int head_room;
  170. /* First: exthdrs may take lots of space (~8K for now)
  171. MAX_HEADER is not enough.
  172. */
  173. head_room = opt->opt_nflen + opt->opt_flen;
  174. seg_len += head_room;
  175. head_room += sizeof(struct ipv6hdr) + LL_RESERVED_SPACE(dst->dev);
  176. if (skb_headroom(skb) < head_room) {
  177. struct sk_buff *skb2 = skb_realloc_headroom(skb, head_room);
  178. if (skb2 == NULL) {
  179. IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
  180. IPSTATS_MIB_OUTDISCARDS);
  181. kfree_skb(skb);
  182. return -ENOBUFS;
  183. }
  184. kfree_skb(skb);
  185. skb = skb2;
  186. skb_set_owner_w(skb, sk);
  187. }
  188. if (opt->opt_flen)
  189. ipv6_push_frag_opts(skb, opt, &proto);
  190. if (opt->opt_nflen)
  191. ipv6_push_nfrag_opts(skb, opt, &proto, &first_hop);
  192. }
  193. skb_push(skb, sizeof(struct ipv6hdr));
  194. skb_reset_network_header(skb);
  195. hdr = ipv6_hdr(skb);
  196. /*
  197. * Fill in the IPv6 header
  198. */
  199. if (np) {
  200. tclass = np->tclass;
  201. hlimit = np->hop_limit;
  202. }
  203. if (hlimit < 0)
  204. hlimit = ip6_dst_hoplimit(dst);
  205. *(__be32 *)hdr = htonl(0x60000000 | (tclass << 20)) | fl6->flowlabel;
  206. hdr->payload_len = htons(seg_len);
  207. hdr->nexthdr = proto;
  208. hdr->hop_limit = hlimit;
  209. ipv6_addr_copy(&hdr->saddr, &fl6->saddr);
  210. ipv6_addr_copy(&hdr->daddr, first_hop);
  211. skb->priority = sk->sk_priority;
  212. skb->mark = sk->sk_mark;
  213. mtu = dst_mtu(dst);
  214. if ((skb->len <= mtu) || skb->local_df || skb_is_gso(skb)) {
  215. IP6_UPD_PO_STATS(net, ip6_dst_idev(skb_dst(skb)),
  216. IPSTATS_MIB_OUT, skb->len);
  217. return NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT, skb, NULL,
  218. dst->dev, dst_output);
  219. }
  220. if (net_ratelimit())
  221. printk(KERN_DEBUG "IPv6: sending pkt_too_big to self\n");
  222. skb->dev = dst->dev;
  223. icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
  224. IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_FRAGFAILS);
  225. kfree_skb(skb);
  226. return -EMSGSIZE;
  227. }
  228. EXPORT_SYMBOL(ip6_xmit);
  229. /*
  230. * To avoid extra problems ND packets are send through this
  231. * routine. It's code duplication but I really want to avoid
  232. * extra checks since ipv6_build_header is used by TCP (which
  233. * is for us performance critical)
  234. */
  235. int ip6_nd_hdr(struct sock *sk, struct sk_buff *skb, struct net_device *dev,
  236. const struct in6_addr *saddr, const struct in6_addr *daddr,
  237. int proto, int len)
  238. {
  239. struct ipv6_pinfo *np = inet6_sk(sk);
  240. struct ipv6hdr *hdr;
  241. skb->protocol = htons(ETH_P_IPV6);
  242. skb->dev = dev;
  243. skb_reset_network_header(skb);
  244. skb_put(skb, sizeof(struct ipv6hdr));
  245. hdr = ipv6_hdr(skb);
  246. *(__be32*)hdr = htonl(0x60000000);
  247. hdr->payload_len = htons(len);
  248. hdr->nexthdr = proto;
  249. hdr->hop_limit = np->hop_limit;
  250. ipv6_addr_copy(&hdr->saddr, saddr);
  251. ipv6_addr_copy(&hdr->daddr, daddr);
  252. return 0;
  253. }
  254. static int ip6_call_ra_chain(struct sk_buff *skb, int sel)
  255. {
  256. struct ip6_ra_chain *ra;
  257. struct sock *last = NULL;
  258. read_lock(&ip6_ra_lock);
  259. for (ra = ip6_ra_chain; ra; ra = ra->next) {
  260. struct sock *sk = ra->sk;
  261. if (sk && ra->sel == sel &&
  262. (!sk->sk_bound_dev_if ||
  263. sk->sk_bound_dev_if == skb->dev->ifindex)) {
  264. if (last) {
  265. struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
  266. if (skb2)
  267. rawv6_rcv(last, skb2);
  268. }
  269. last = sk;
  270. }
  271. }
  272. if (last) {
  273. rawv6_rcv(last, skb);
  274. read_unlock(&ip6_ra_lock);
  275. return 1;
  276. }
  277. read_unlock(&ip6_ra_lock);
  278. return 0;
  279. }
  280. static int ip6_forward_proxy_check(struct sk_buff *skb)
  281. {
  282. struct ipv6hdr *hdr = ipv6_hdr(skb);
  283. u8 nexthdr = hdr->nexthdr;
  284. int offset;
  285. if (ipv6_ext_hdr(nexthdr)) {
  286. offset = ipv6_skip_exthdr(skb, sizeof(*hdr), &nexthdr);
  287. if (offset < 0)
  288. return 0;
  289. } else
  290. offset = sizeof(struct ipv6hdr);
  291. if (nexthdr == IPPROTO_ICMPV6) {
  292. struct icmp6hdr *icmp6;
  293. if (!pskb_may_pull(skb, (skb_network_header(skb) +
  294. offset + 1 - skb->data)))
  295. return 0;
  296. icmp6 = (struct icmp6hdr *)(skb_network_header(skb) + offset);
  297. switch (icmp6->icmp6_type) {
  298. case NDISC_ROUTER_SOLICITATION:
  299. case NDISC_ROUTER_ADVERTISEMENT:
  300. case NDISC_NEIGHBOUR_SOLICITATION:
  301. case NDISC_NEIGHBOUR_ADVERTISEMENT:
  302. case NDISC_REDIRECT:
  303. /* For reaction involving unicast neighbor discovery
  304. * message destined to the proxied address, pass it to
  305. * input function.
  306. */
  307. return 1;
  308. default:
  309. break;
  310. }
  311. }
  312. /*
  313. * The proxying router can't forward traffic sent to a link-local
  314. * address, so signal the sender and discard the packet. This
  315. * behavior is clarified by the MIPv6 specification.
  316. */
  317. if (ipv6_addr_type(&hdr->daddr) & IPV6_ADDR_LINKLOCAL) {
  318. dst_link_failure(skb);
  319. return -1;
  320. }
  321. return 0;
  322. }
  323. static inline int ip6_forward_finish(struct sk_buff *skb)
  324. {
  325. return dst_output(skb);
  326. }
  327. int ip6_forward(struct sk_buff *skb)
  328. {
  329. struct dst_entry *dst = skb_dst(skb);
  330. struct ipv6hdr *hdr = ipv6_hdr(skb);
  331. struct inet6_skb_parm *opt = IP6CB(skb);
  332. struct net *net = dev_net(dst->dev);
  333. struct neighbour *n;
  334. u32 mtu;
  335. if (net->ipv6.devconf_all->forwarding == 0)
  336. goto error;
  337. if (skb_warn_if_lro(skb))
  338. goto drop;
  339. if (!xfrm6_policy_check(NULL, XFRM_POLICY_FWD, skb)) {
  340. IP6_INC_STATS(net, ip6_dst_idev(dst), IPSTATS_MIB_INDISCARDS);
  341. goto drop;
  342. }
  343. if (skb->pkt_type != PACKET_HOST)
  344. goto drop;
  345. skb_forward_csum(skb);
  346. /*
  347. * We DO NOT make any processing on
  348. * RA packets, pushing them to user level AS IS
  349. * without ane WARRANTY that application will be able
  350. * to interpret them. The reason is that we
  351. * cannot make anything clever here.
  352. *
  353. * We are not end-node, so that if packet contains
  354. * AH/ESP, we cannot make anything.
  355. * Defragmentation also would be mistake, RA packets
  356. * cannot be fragmented, because there is no warranty
  357. * that different fragments will go along one path. --ANK
  358. */
  359. if (opt->ra) {
  360. u8 *ptr = skb_network_header(skb) + opt->ra;
  361. if (ip6_call_ra_chain(skb, (ptr[2]<<8) + ptr[3]))
  362. return 0;
  363. }
  364. /*
  365. * check and decrement ttl
  366. */
  367. if (hdr->hop_limit <= 1) {
  368. /* Force OUTPUT device used as source address */
  369. skb->dev = dst->dev;
  370. icmpv6_send(skb, ICMPV6_TIME_EXCEED, ICMPV6_EXC_HOPLIMIT, 0);
  371. IP6_INC_STATS_BH(net,
  372. ip6_dst_idev(dst), IPSTATS_MIB_INHDRERRORS);
  373. kfree_skb(skb);
  374. return -ETIMEDOUT;
  375. }
  376. /* XXX: idev->cnf.proxy_ndp? */
  377. if (net->ipv6.devconf_all->proxy_ndp &&
  378. pneigh_lookup(&nd_tbl, net, &hdr->daddr, skb->dev, 0)) {
  379. int proxied = ip6_forward_proxy_check(skb);
  380. if (proxied > 0)
  381. return ip6_input(skb);
  382. else if (proxied < 0) {
  383. IP6_INC_STATS(net, ip6_dst_idev(dst),
  384. IPSTATS_MIB_INDISCARDS);
  385. goto drop;
  386. }
  387. }
  388. if (!xfrm6_route_forward(skb)) {
  389. IP6_INC_STATS(net, ip6_dst_idev(dst), IPSTATS_MIB_INDISCARDS);
  390. goto drop;
  391. }
  392. dst = skb_dst(skb);
  393. /* IPv6 specs say nothing about it, but it is clear that we cannot
  394. send redirects to source routed frames.
  395. We don't send redirects to frames decapsulated from IPsec.
  396. */
  397. n = dst_get_neighbour(dst);
  398. if (skb->dev == dst->dev && n && opt->srcrt == 0 && !skb_sec_path(skb)) {
  399. struct in6_addr *target = NULL;
  400. struct rt6_info *rt;
  401. /*
  402. * incoming and outgoing devices are the same
  403. * send a redirect.
  404. */
  405. rt = (struct rt6_info *) dst;
  406. if ((rt->rt6i_flags & RTF_GATEWAY))
  407. target = (struct in6_addr*)&n->primary_key;
  408. else
  409. target = &hdr->daddr;
  410. if (!rt->rt6i_peer)
  411. rt6_bind_peer(rt, 1);
  412. /* Limit redirects both by destination (here)
  413. and by source (inside ndisc_send_redirect)
  414. */
  415. if (inet_peer_xrlim_allow(rt->rt6i_peer, 1*HZ))
  416. ndisc_send_redirect(skb, n, target);
  417. } else {
  418. int addrtype = ipv6_addr_type(&hdr->saddr);
  419. /* This check is security critical. */
  420. if (addrtype == IPV6_ADDR_ANY ||
  421. addrtype & (IPV6_ADDR_MULTICAST | IPV6_ADDR_LOOPBACK))
  422. goto error;
  423. if (addrtype & IPV6_ADDR_LINKLOCAL) {
  424. icmpv6_send(skb, ICMPV6_DEST_UNREACH,
  425. ICMPV6_NOT_NEIGHBOUR, 0);
  426. goto error;
  427. }
  428. }
  429. mtu = dst_mtu(dst);
  430. if (mtu < IPV6_MIN_MTU)
  431. mtu = IPV6_MIN_MTU;
  432. if (skb->len > mtu && !skb_is_gso(skb)) {
  433. /* Again, force OUTPUT device used as source address */
  434. skb->dev = dst->dev;
  435. icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
  436. IP6_INC_STATS_BH(net,
  437. ip6_dst_idev(dst), IPSTATS_MIB_INTOOBIGERRORS);
  438. IP6_INC_STATS_BH(net,
  439. ip6_dst_idev(dst), IPSTATS_MIB_FRAGFAILS);
  440. kfree_skb(skb);
  441. return -EMSGSIZE;
  442. }
  443. if (skb_cow(skb, dst->dev->hard_header_len)) {
  444. IP6_INC_STATS(net, ip6_dst_idev(dst), IPSTATS_MIB_OUTDISCARDS);
  445. goto drop;
  446. }
  447. hdr = ipv6_hdr(skb);
  448. /* Mangling hops number delayed to point after skb COW */
  449. hdr->hop_limit--;
  450. IP6_INC_STATS_BH(net, ip6_dst_idev(dst), IPSTATS_MIB_OUTFORWDATAGRAMS);
  451. return NF_HOOK(NFPROTO_IPV6, NF_INET_FORWARD, skb, skb->dev, dst->dev,
  452. ip6_forward_finish);
  453. error:
  454. IP6_INC_STATS_BH(net, ip6_dst_idev(dst), IPSTATS_MIB_INADDRERRORS);
  455. drop:
  456. kfree_skb(skb);
  457. return -EINVAL;
  458. }
  459. static void ip6_copy_metadata(struct sk_buff *to, struct sk_buff *from)
  460. {
  461. to->pkt_type = from->pkt_type;
  462. to->priority = from->priority;
  463. to->protocol = from->protocol;
  464. skb_dst_drop(to);
  465. skb_dst_set(to, dst_clone(skb_dst(from)));
  466. to->dev = from->dev;
  467. to->mark = from->mark;
  468. #ifdef CONFIG_NET_SCHED
  469. to->tc_index = from->tc_index;
  470. #endif
  471. nf_copy(to, from);
  472. #if defined(CONFIG_NETFILTER_XT_TARGET_TRACE) || \
  473. defined(CONFIG_NETFILTER_XT_TARGET_TRACE_MODULE)
  474. to->nf_trace = from->nf_trace;
  475. #endif
  476. skb_copy_secmark(to, from);
  477. }
  478. int ip6_find_1stfragopt(struct sk_buff *skb, u8 **nexthdr)
  479. {
  480. u16 offset = sizeof(struct ipv6hdr);
  481. struct ipv6_opt_hdr *exthdr =
  482. (struct ipv6_opt_hdr *)(ipv6_hdr(skb) + 1);
  483. unsigned int packet_len = skb->tail - skb->network_header;
  484. int found_rhdr = 0;
  485. *nexthdr = &ipv6_hdr(skb)->nexthdr;
  486. while (offset + 1 <= packet_len) {
  487. switch (**nexthdr) {
  488. case NEXTHDR_HOP:
  489. break;
  490. case NEXTHDR_ROUTING:
  491. found_rhdr = 1;
  492. break;
  493. case NEXTHDR_DEST:
  494. #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
  495. if (ipv6_find_tlv(skb, offset, IPV6_TLV_HAO) >= 0)
  496. break;
  497. #endif
  498. if (found_rhdr)
  499. return offset;
  500. break;
  501. default :
  502. return offset;
  503. }
  504. offset += ipv6_optlen(exthdr);
  505. *nexthdr = &exthdr->nexthdr;
  506. exthdr = (struct ipv6_opt_hdr *)(skb_network_header(skb) +
  507. offset);
  508. }
  509. return offset;
  510. }
  511. void ipv6_select_ident(struct frag_hdr *fhdr, struct rt6_info *rt)
  512. {
  513. static atomic_t ipv6_fragmentation_id;
  514. int old, new;
  515. if (rt) {
  516. struct inet_peer *peer;
  517. if (!rt->rt6i_peer)
  518. rt6_bind_peer(rt, 1);
  519. peer = rt->rt6i_peer;
  520. if (peer) {
  521. fhdr->identification = htonl(inet_getid(peer, 0));
  522. return;
  523. }
  524. }
  525. do {
  526. old = atomic_read(&ipv6_fragmentation_id);
  527. new = old + 1;
  528. if (!new)
  529. new = 1;
  530. } while (atomic_cmpxchg(&ipv6_fragmentation_id, old, new) != old);
  531. fhdr->identification = htonl(new);
  532. }
  533. int ip6_fragment(struct sk_buff *skb, int (*output)(struct sk_buff *))
  534. {
  535. struct sk_buff *frag;
  536. struct rt6_info *rt = (struct rt6_info*)skb_dst(skb);
  537. struct ipv6_pinfo *np = skb->sk ? inet6_sk(skb->sk) : NULL;
  538. struct ipv6hdr *tmp_hdr;
  539. struct frag_hdr *fh;
  540. unsigned int mtu, hlen, left, len;
  541. __be32 frag_id = 0;
  542. int ptr, offset = 0, err=0;
  543. u8 *prevhdr, nexthdr = 0;
  544. struct net *net = dev_net(skb_dst(skb)->dev);
  545. hlen = ip6_find_1stfragopt(skb, &prevhdr);
  546. nexthdr = *prevhdr;
  547. mtu = ip6_skb_dst_mtu(skb);
  548. /* We must not fragment if the socket is set to force MTU discovery
  549. * or if the skb it not generated by a local socket.
  550. */
  551. if (!skb->local_df && skb->len > mtu) {
  552. skb->dev = skb_dst(skb)->dev;
  553. icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
  554. IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
  555. IPSTATS_MIB_FRAGFAILS);
  556. kfree_skb(skb);
  557. return -EMSGSIZE;
  558. }
  559. if (np && np->frag_size < mtu) {
  560. if (np->frag_size)
  561. mtu = np->frag_size;
  562. }
  563. mtu -= hlen + sizeof(struct frag_hdr);
  564. if (skb_has_frag_list(skb)) {
  565. int first_len = skb_pagelen(skb);
  566. struct sk_buff *frag2;
  567. if (first_len - hlen > mtu ||
  568. ((first_len - hlen) & 7) ||
  569. skb_cloned(skb))
  570. goto slow_path;
  571. skb_walk_frags(skb, frag) {
  572. /* Correct geometry. */
  573. if (frag->len > mtu ||
  574. ((frag->len & 7) && frag->next) ||
  575. skb_headroom(frag) < hlen)
  576. goto slow_path_clean;
  577. /* Partially cloned skb? */
  578. if (skb_shared(frag))
  579. goto slow_path_clean;
  580. BUG_ON(frag->sk);
  581. if (skb->sk) {
  582. frag->sk = skb->sk;
  583. frag->destructor = sock_wfree;
  584. }
  585. skb->truesize -= frag->truesize;
  586. }
  587. err = 0;
  588. offset = 0;
  589. frag = skb_shinfo(skb)->frag_list;
  590. skb_frag_list_init(skb);
  591. /* BUILD HEADER */
  592. *prevhdr = NEXTHDR_FRAGMENT;
  593. tmp_hdr = kmemdup(skb_network_header(skb), hlen, GFP_ATOMIC);
  594. if (!tmp_hdr) {
  595. IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
  596. IPSTATS_MIB_FRAGFAILS);
  597. return -ENOMEM;
  598. }
  599. __skb_pull(skb, hlen);
  600. fh = (struct frag_hdr*)__skb_push(skb, sizeof(struct frag_hdr));
  601. __skb_push(skb, hlen);
  602. skb_reset_network_header(skb);
  603. memcpy(skb_network_header(skb), tmp_hdr, hlen);
  604. ipv6_select_ident(fh, rt);
  605. fh->nexthdr = nexthdr;
  606. fh->reserved = 0;
  607. fh->frag_off = htons(IP6_MF);
  608. frag_id = fh->identification;
  609. first_len = skb_pagelen(skb);
  610. skb->data_len = first_len - skb_headlen(skb);
  611. skb->len = first_len;
  612. ipv6_hdr(skb)->payload_len = htons(first_len -
  613. sizeof(struct ipv6hdr));
  614. dst_hold(&rt->dst);
  615. for (;;) {
  616. /* Prepare header of the next frame,
  617. * before previous one went down. */
  618. if (frag) {
  619. frag->ip_summed = CHECKSUM_NONE;
  620. skb_reset_transport_header(frag);
  621. fh = (struct frag_hdr*)__skb_push(frag, sizeof(struct frag_hdr));
  622. __skb_push(frag, hlen);
  623. skb_reset_network_header(frag);
  624. memcpy(skb_network_header(frag), tmp_hdr,
  625. hlen);
  626. offset += skb->len - hlen - sizeof(struct frag_hdr);
  627. fh->nexthdr = nexthdr;
  628. fh->reserved = 0;
  629. fh->frag_off = htons(offset);
  630. if (frag->next != NULL)
  631. fh->frag_off |= htons(IP6_MF);
  632. fh->identification = frag_id;
  633. ipv6_hdr(frag)->payload_len =
  634. htons(frag->len -
  635. sizeof(struct ipv6hdr));
  636. ip6_copy_metadata(frag, skb);
  637. }
  638. err = output(skb);
  639. if(!err)
  640. IP6_INC_STATS(net, ip6_dst_idev(&rt->dst),
  641. IPSTATS_MIB_FRAGCREATES);
  642. if (err || !frag)
  643. break;
  644. skb = frag;
  645. frag = skb->next;
  646. skb->next = NULL;
  647. }
  648. kfree(tmp_hdr);
  649. if (err == 0) {
  650. IP6_INC_STATS(net, ip6_dst_idev(&rt->dst),
  651. IPSTATS_MIB_FRAGOKS);
  652. dst_release(&rt->dst);
  653. return 0;
  654. }
  655. while (frag) {
  656. skb = frag->next;
  657. kfree_skb(frag);
  658. frag = skb;
  659. }
  660. IP6_INC_STATS(net, ip6_dst_idev(&rt->dst),
  661. IPSTATS_MIB_FRAGFAILS);
  662. dst_release(&rt->dst);
  663. return err;
  664. slow_path_clean:
  665. skb_walk_frags(skb, frag2) {
  666. if (frag2 == frag)
  667. break;
  668. frag2->sk = NULL;
  669. frag2->destructor = NULL;
  670. skb->truesize += frag2->truesize;
  671. }
  672. }
  673. slow_path:
  674. left = skb->len - hlen; /* Space per frame */
  675. ptr = hlen; /* Where to start from */
  676. /*
  677. * Fragment the datagram.
  678. */
  679. *prevhdr = NEXTHDR_FRAGMENT;
  680. /*
  681. * Keep copying data until we run out.
  682. */
  683. while(left > 0) {
  684. len = left;
  685. /* IF: it doesn't fit, use 'mtu' - the data space left */
  686. if (len > mtu)
  687. len = mtu;
  688. /* IF: we are not sending up to and including the packet end
  689. then align the next start on an eight byte boundary */
  690. if (len < left) {
  691. len &= ~7;
  692. }
  693. /*
  694. * Allocate buffer.
  695. */
  696. if ((frag = alloc_skb(len+hlen+sizeof(struct frag_hdr)+LL_ALLOCATED_SPACE(rt->dst.dev), GFP_ATOMIC)) == NULL) {
  697. NETDEBUG(KERN_INFO "IPv6: frag: no memory for new fragment!\n");
  698. IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
  699. IPSTATS_MIB_FRAGFAILS);
  700. err = -ENOMEM;
  701. goto fail;
  702. }
  703. /*
  704. * Set up data on packet
  705. */
  706. ip6_copy_metadata(frag, skb);
  707. skb_reserve(frag, LL_RESERVED_SPACE(rt->dst.dev));
  708. skb_put(frag, len + hlen + sizeof(struct frag_hdr));
  709. skb_reset_network_header(frag);
  710. fh = (struct frag_hdr *)(skb_network_header(frag) + hlen);
  711. frag->transport_header = (frag->network_header + hlen +
  712. sizeof(struct frag_hdr));
  713. /*
  714. * Charge the memory for the fragment to any owner
  715. * it might possess
  716. */
  717. if (skb->sk)
  718. skb_set_owner_w(frag, skb->sk);
  719. /*
  720. * Copy the packet header into the new buffer.
  721. */
  722. skb_copy_from_linear_data(skb, skb_network_header(frag), hlen);
  723. /*
  724. * Build fragment header.
  725. */
  726. fh->nexthdr = nexthdr;
  727. fh->reserved = 0;
  728. if (!frag_id) {
  729. ipv6_select_ident(fh, rt);
  730. frag_id = fh->identification;
  731. } else
  732. fh->identification = frag_id;
  733. /*
  734. * Copy a block of the IP datagram.
  735. */
  736. if (skb_copy_bits(skb, ptr, skb_transport_header(frag), len))
  737. BUG();
  738. left -= len;
  739. fh->frag_off = htons(offset);
  740. if (left > 0)
  741. fh->frag_off |= htons(IP6_MF);
  742. ipv6_hdr(frag)->payload_len = htons(frag->len -
  743. sizeof(struct ipv6hdr));
  744. ptr += len;
  745. offset += len;
  746. /*
  747. * Put this fragment into the sending queue.
  748. */
  749. err = output(frag);
  750. if (err)
  751. goto fail;
  752. IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
  753. IPSTATS_MIB_FRAGCREATES);
  754. }
  755. IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
  756. IPSTATS_MIB_FRAGOKS);
  757. kfree_skb(skb);
  758. return err;
  759. fail:
  760. IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
  761. IPSTATS_MIB_FRAGFAILS);
  762. kfree_skb(skb);
  763. return err;
  764. }
  765. static inline int ip6_rt_check(const struct rt6key *rt_key,
  766. const struct in6_addr *fl_addr,
  767. const struct in6_addr *addr_cache)
  768. {
  769. return (rt_key->plen != 128 || !ipv6_addr_equal(fl_addr, &rt_key->addr)) &&
  770. (addr_cache == NULL || !ipv6_addr_equal(fl_addr, addr_cache));
  771. }
  772. static struct dst_entry *ip6_sk_dst_check(struct sock *sk,
  773. struct dst_entry *dst,
  774. const struct flowi6 *fl6)
  775. {
  776. struct ipv6_pinfo *np = inet6_sk(sk);
  777. struct rt6_info *rt = (struct rt6_info *)dst;
  778. if (!dst)
  779. goto out;
  780. /* Yes, checking route validity in not connected
  781. * case is not very simple. Take into account,
  782. * that we do not support routing by source, TOS,
  783. * and MSG_DONTROUTE --ANK (980726)
  784. *
  785. * 1. ip6_rt_check(): If route was host route,
  786. * check that cached destination is current.
  787. * If it is network route, we still may
  788. * check its validity using saved pointer
  789. * to the last used address: daddr_cache.
  790. * We do not want to save whole address now,
  791. * (because main consumer of this service
  792. * is tcp, which has not this problem),
  793. * so that the last trick works only on connected
  794. * sockets.
  795. * 2. oif also should be the same.
  796. */
  797. if (ip6_rt_check(&rt->rt6i_dst, &fl6->daddr, np->daddr_cache) ||
  798. #ifdef CONFIG_IPV6_SUBTREES
  799. ip6_rt_check(&rt->rt6i_src, &fl6->saddr, np->saddr_cache) ||
  800. #endif
  801. (fl6->flowi6_oif && fl6->flowi6_oif != dst->dev->ifindex)) {
  802. dst_release(dst);
  803. dst = NULL;
  804. }
  805. out:
  806. return dst;
  807. }
  808. static int ip6_dst_lookup_tail(struct sock *sk,
  809. struct dst_entry **dst, struct flowi6 *fl6)
  810. {
  811. struct net *net = sock_net(sk);
  812. #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
  813. struct neighbour *n;
  814. #endif
  815. int err;
  816. if (*dst == NULL)
  817. *dst = ip6_route_output(net, sk, fl6);
  818. if ((err = (*dst)->error))
  819. goto out_err_release;
  820. if (ipv6_addr_any(&fl6->saddr)) {
  821. struct rt6_info *rt = (struct rt6_info *) *dst;
  822. err = ip6_route_get_saddr(net, rt, &fl6->daddr,
  823. sk ? inet6_sk(sk)->srcprefs : 0,
  824. &fl6->saddr);
  825. if (err)
  826. goto out_err_release;
  827. }
  828. #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
  829. /*
  830. * Here if the dst entry we've looked up
  831. * has a neighbour entry that is in the INCOMPLETE
  832. * state and the src address from the flow is
  833. * marked as OPTIMISTIC, we release the found
  834. * dst entry and replace it instead with the
  835. * dst entry of the nexthop router
  836. */
  837. rcu_read_lock();
  838. n = dst_get_neighbour(*dst);
  839. if (n && !(n->nud_state & NUD_VALID)) {
  840. struct inet6_ifaddr *ifp;
  841. struct flowi6 fl_gw6;
  842. int redirect;
  843. rcu_read_unlock();
  844. ifp = ipv6_get_ifaddr(net, &fl6->saddr,
  845. (*dst)->dev, 1);
  846. redirect = (ifp && ifp->flags & IFA_F_OPTIMISTIC);
  847. if (ifp)
  848. in6_ifa_put(ifp);
  849. if (redirect) {
  850. /*
  851. * We need to get the dst entry for the
  852. * default router instead
  853. */
  854. dst_release(*dst);
  855. memcpy(&fl_gw6, fl6, sizeof(struct flowi6));
  856. memset(&fl_gw6.daddr, 0, sizeof(struct in6_addr));
  857. *dst = ip6_route_output(net, sk, &fl_gw6);
  858. if ((err = (*dst)->error))
  859. goto out_err_release;
  860. }
  861. } else {
  862. rcu_read_unlock();
  863. }
  864. #endif
  865. return 0;
  866. out_err_release:
  867. if (err == -ENETUNREACH)
  868. IP6_INC_STATS_BH(net, NULL, IPSTATS_MIB_OUTNOROUTES);
  869. dst_release(*dst);
  870. *dst = NULL;
  871. return err;
  872. }
  873. /**
  874. * ip6_dst_lookup - perform route lookup on flow
  875. * @sk: socket which provides route info
  876. * @dst: pointer to dst_entry * for result
  877. * @fl6: flow to lookup
  878. *
  879. * This function performs a route lookup on the given flow.
  880. *
  881. * It returns zero on success, or a standard errno code on error.
  882. */
  883. int ip6_dst_lookup(struct sock *sk, struct dst_entry **dst, struct flowi6 *fl6)
  884. {
  885. *dst = NULL;
  886. return ip6_dst_lookup_tail(sk, dst, fl6);
  887. }
  888. EXPORT_SYMBOL_GPL(ip6_dst_lookup);
  889. /**
  890. * ip6_dst_lookup_flow - perform route lookup on flow with ipsec
  891. * @sk: socket which provides route info
  892. * @fl6: flow to lookup
  893. * @final_dst: final destination address for ipsec lookup
  894. * @can_sleep: we are in a sleepable context
  895. *
  896. * This function performs a route lookup on the given flow.
  897. *
  898. * It returns a valid dst pointer on success, or a pointer encoded
  899. * error code.
  900. */
  901. struct dst_entry *ip6_dst_lookup_flow(struct sock *sk, struct flowi6 *fl6,
  902. const struct in6_addr *final_dst,
  903. bool can_sleep)
  904. {
  905. struct dst_entry *dst = NULL;
  906. int err;
  907. err = ip6_dst_lookup_tail(sk, &dst, fl6);
  908. if (err)
  909. return ERR_PTR(err);
  910. if (final_dst)
  911. ipv6_addr_copy(&fl6->daddr, final_dst);
  912. if (can_sleep)
  913. fl6->flowi6_flags |= FLOWI_FLAG_CAN_SLEEP;
  914. return xfrm_lookup(sock_net(sk), dst, flowi6_to_flowi(fl6), sk, 0);
  915. }
  916. EXPORT_SYMBOL_GPL(ip6_dst_lookup_flow);
  917. /**
  918. * ip6_sk_dst_lookup_flow - perform socket cached route lookup on flow
  919. * @sk: socket which provides the dst cache and route info
  920. * @fl6: flow to lookup
  921. * @final_dst: final destination address for ipsec lookup
  922. * @can_sleep: we are in a sleepable context
  923. *
  924. * This function performs a route lookup on the given flow with the
  925. * possibility of using the cached route in the socket if it is valid.
  926. * It will take the socket dst lock when operating on the dst cache.
  927. * As a result, this function can only be used in process context.
  928. *
  929. * It returns a valid dst pointer on success, or a pointer encoded
  930. * error code.
  931. */
  932. struct dst_entry *ip6_sk_dst_lookup_flow(struct sock *sk, struct flowi6 *fl6,
  933. const struct in6_addr *final_dst,
  934. bool can_sleep)
  935. {
  936. struct dst_entry *dst = sk_dst_check(sk, inet6_sk(sk)->dst_cookie);
  937. int err;
  938. dst = ip6_sk_dst_check(sk, dst, fl6);
  939. err = ip6_dst_lookup_tail(sk, &dst, fl6);
  940. if (err)
  941. return ERR_PTR(err);
  942. if (final_dst)
  943. ipv6_addr_copy(&fl6->daddr, final_dst);
  944. if (can_sleep)
  945. fl6->flowi6_flags |= FLOWI_FLAG_CAN_SLEEP;
  946. return xfrm_lookup(sock_net(sk), dst, flowi6_to_flowi(fl6), sk, 0);
  947. }
  948. EXPORT_SYMBOL_GPL(ip6_sk_dst_lookup_flow);
  949. static inline int ip6_ufo_append_data(struct sock *sk,
  950. int getfrag(void *from, char *to, int offset, int len,
  951. int odd, struct sk_buff *skb),
  952. void *from, int length, int hh_len, int fragheaderlen,
  953. int transhdrlen, int mtu,unsigned int flags,
  954. struct rt6_info *rt)
  955. {
  956. struct sk_buff *skb;
  957. int err;
  958. /* There is support for UDP large send offload by network
  959. * device, so create one single skb packet containing complete
  960. * udp datagram
  961. */
  962. if ((skb = skb_peek_tail(&sk->sk_write_queue)) == NULL) {
  963. skb = sock_alloc_send_skb(sk,
  964. hh_len + fragheaderlen + transhdrlen + 20,
  965. (flags & MSG_DONTWAIT), &err);
  966. if (skb == NULL)
  967. return -ENOMEM;
  968. /* reserve space for Hardware header */
  969. skb_reserve(skb, hh_len);
  970. /* create space for UDP/IP header */
  971. skb_put(skb,fragheaderlen + transhdrlen);
  972. /* initialize network header pointer */
  973. skb_reset_network_header(skb);
  974. /* initialize protocol header pointer */
  975. skb->transport_header = skb->network_header + fragheaderlen;
  976. skb->ip_summed = CHECKSUM_PARTIAL;
  977. skb->csum = 0;
  978. }
  979. err = skb_append_datato_frags(sk,skb, getfrag, from,
  980. (length - transhdrlen));
  981. if (!err) {
  982. struct frag_hdr fhdr;
  983. /* Specify the length of each IPv6 datagram fragment.
  984. * It has to be a multiple of 8.
  985. */
  986. skb_shinfo(skb)->gso_size = (mtu - fragheaderlen -
  987. sizeof(struct frag_hdr)) & ~7;
  988. skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
  989. ipv6_select_ident(&fhdr, rt);
  990. skb_shinfo(skb)->ip6_frag_id = fhdr.identification;
  991. __skb_queue_tail(&sk->sk_write_queue, skb);
  992. return 0;
  993. }
  994. /* There is not enough support do UPD LSO,
  995. * so follow normal path
  996. */
  997. kfree_skb(skb);
  998. return err;
  999. }
  1000. static inline struct ipv6_opt_hdr *ip6_opt_dup(struct ipv6_opt_hdr *src,
  1001. gfp_t gfp)
  1002. {
  1003. return src ? kmemdup(src, (src->hdrlen + 1) * 8, gfp) : NULL;
  1004. }
  1005. static inline struct ipv6_rt_hdr *ip6_rthdr_dup(struct ipv6_rt_hdr *src,
  1006. gfp_t gfp)
  1007. {
  1008. return src ? kmemdup(src, (src->hdrlen + 1) * 8, gfp) : NULL;
  1009. }
  1010. int ip6_append_data(struct sock *sk, int getfrag(void *from, char *to,
  1011. int offset, int len, int odd, struct sk_buff *skb),
  1012. void *from, int length, int transhdrlen,
  1013. int hlimit, int tclass, struct ipv6_txoptions *opt, struct flowi6 *fl6,
  1014. struct rt6_info *rt, unsigned int flags, int dontfrag)
  1015. {
  1016. struct inet_sock *inet = inet_sk(sk);
  1017. struct ipv6_pinfo *np = inet6_sk(sk);
  1018. struct inet_cork *cork;
  1019. struct sk_buff *skb;
  1020. unsigned int maxfraglen, fragheaderlen;
  1021. int exthdrlen;
  1022. int hh_len;
  1023. int mtu;
  1024. int copy;
  1025. int err;
  1026. int offset = 0;
  1027. int csummode = CHECKSUM_NONE;
  1028. __u8 tx_flags = 0;
  1029. if (flags&MSG_PROBE)
  1030. return 0;
  1031. cork = &inet->cork.base;
  1032. if (skb_queue_empty(&sk->sk_write_queue)) {
  1033. /*
  1034. * setup for corking
  1035. */
  1036. if (opt) {
  1037. if (WARN_ON(np->cork.opt))
  1038. return -EINVAL;
  1039. np->cork.opt = kmalloc(opt->tot_len, sk->sk_allocation);
  1040. if (unlikely(np->cork.opt == NULL))
  1041. return -ENOBUFS;
  1042. np->cork.opt->tot_len = opt->tot_len;
  1043. np->cork.opt->opt_flen = opt->opt_flen;
  1044. np->cork.opt->opt_nflen = opt->opt_nflen;
  1045. np->cork.opt->dst0opt = ip6_opt_dup(opt->dst0opt,
  1046. sk->sk_allocation);
  1047. if (opt->dst0opt && !np->cork.opt->dst0opt)
  1048. return -ENOBUFS;
  1049. np->cork.opt->dst1opt = ip6_opt_dup(opt->dst1opt,
  1050. sk->sk_allocation);
  1051. if (opt->dst1opt && !np->cork.opt->dst1opt)
  1052. return -ENOBUFS;
  1053. np->cork.opt->hopopt = ip6_opt_dup(opt->hopopt,
  1054. sk->sk_allocation);
  1055. if (opt->hopopt && !np->cork.opt->hopopt)
  1056. return -ENOBUFS;
  1057. np->cork.opt->srcrt = ip6_rthdr_dup(opt->srcrt,
  1058. sk->sk_allocation);
  1059. if (opt->srcrt && !np->cork.opt->srcrt)
  1060. return -ENOBUFS;
  1061. /* need source address above miyazawa*/
  1062. }
  1063. dst_hold(&rt->dst);
  1064. cork->dst = &rt->dst;
  1065. inet->cork.fl.u.ip6 = *fl6;
  1066. np->cork.hop_limit = hlimit;
  1067. np->cork.tclass = tclass;
  1068. mtu = np->pmtudisc == IPV6_PMTUDISC_PROBE ?
  1069. rt->dst.dev->mtu : dst_mtu(rt->dst.path);
  1070. if (np->frag_size < mtu) {
  1071. if (np->frag_size)
  1072. mtu = np->frag_size;
  1073. }
  1074. cork->fragsize = mtu;
  1075. if (dst_allfrag(rt->dst.path))
  1076. cork->flags |= IPCORK_ALLFRAG;
  1077. cork->length = 0;
  1078. sk->sk_sndmsg_page = NULL;
  1079. sk->sk_sndmsg_off = 0;
  1080. exthdrlen = rt->dst.header_len + (opt ? opt->opt_flen : 0) -
  1081. rt->rt6i_nfheader_len;
  1082. length += exthdrlen;
  1083. transhdrlen += exthdrlen;
  1084. } else {
  1085. rt = (struct rt6_info *)cork->dst;
  1086. fl6 = &inet->cork.fl.u.ip6;
  1087. opt = np->cork.opt;
  1088. transhdrlen = 0;
  1089. exthdrlen = 0;
  1090. mtu = cork->fragsize;
  1091. }
  1092. hh_len = LL_RESERVED_SPACE(rt->dst.dev);
  1093. fragheaderlen = sizeof(struct ipv6hdr) + rt->rt6i_nfheader_len +
  1094. (opt ? opt->opt_nflen : 0);
  1095. maxfraglen = ((mtu - fragheaderlen) & ~7) + fragheaderlen - sizeof(struct frag_hdr);
  1096. if (mtu <= sizeof(struct ipv6hdr) + IPV6_MAXPLEN) {
  1097. if (cork->length + length > sizeof(struct ipv6hdr) + IPV6_MAXPLEN - fragheaderlen) {
  1098. ipv6_local_error(sk, EMSGSIZE, fl6, mtu-exthdrlen);
  1099. return -EMSGSIZE;
  1100. }
  1101. }
  1102. /* For UDP, check if TX timestamp is enabled */
  1103. if (sk->sk_type == SOCK_DGRAM) {
  1104. err = sock_tx_timestamp(sk, &tx_flags);
  1105. if (err)
  1106. goto error;
  1107. }
  1108. /*
  1109. * Let's try using as much space as possible.
  1110. * Use MTU if total length of the message fits into the MTU.
  1111. * Otherwise, we need to reserve fragment header and
  1112. * fragment alignment (= 8-15 octects, in total).
  1113. *
  1114. * Note that we may need to "move" the data from the tail of
  1115. * of the buffer to the new fragment when we split
  1116. * the message.
  1117. *
  1118. * FIXME: It may be fragmented into multiple chunks
  1119. * at once if non-fragmentable extension headers
  1120. * are too large.
  1121. * --yoshfuji
  1122. */
  1123. cork->length += length;
  1124. if (length > mtu) {
  1125. int proto = sk->sk_protocol;
  1126. if (dontfrag && (proto == IPPROTO_UDP || proto == IPPROTO_RAW)){
  1127. ipv6_local_rxpmtu(sk, fl6, mtu-exthdrlen);
  1128. return -EMSGSIZE;
  1129. }
  1130. if (proto == IPPROTO_UDP &&
  1131. (rt->dst.dev->features & NETIF_F_UFO)) {
  1132. err = ip6_ufo_append_data(sk, getfrag, from, length,
  1133. hh_len, fragheaderlen,
  1134. transhdrlen, mtu, flags, rt);
  1135. if (err)
  1136. goto error;
  1137. return 0;
  1138. }
  1139. }
  1140. if ((skb = skb_peek_tail(&sk->sk_write_queue)) == NULL)
  1141. goto alloc_new_skb;
  1142. while (length > 0) {
  1143. /* Check if the remaining data fits into current packet. */
  1144. copy = (cork->length <= mtu && !(cork->flags & IPCORK_ALLFRAG) ? mtu : maxfraglen) - skb->len;
  1145. if (copy < length)
  1146. copy = maxfraglen - skb->len;
  1147. if (copy <= 0) {
  1148. char *data;
  1149. unsigned int datalen;
  1150. unsigned int fraglen;
  1151. unsigned int fraggap;
  1152. unsigned int alloclen;
  1153. struct sk_buff *skb_prev;
  1154. alloc_new_skb:
  1155. skb_prev = skb;
  1156. /* There's no room in the current skb */
  1157. if (skb_prev)
  1158. fraggap = skb_prev->len - maxfraglen;
  1159. else
  1160. fraggap = 0;
  1161. /*
  1162. * If remaining data exceeds the mtu,
  1163. * we know we need more fragment(s).
  1164. */
  1165. datalen = length + fraggap;
  1166. if (datalen > (cork->length <= mtu && !(cork->flags & IPCORK_ALLFRAG) ? mtu : maxfraglen) - fragheaderlen)
  1167. datalen = maxfraglen - fragheaderlen;
  1168. fraglen = datalen + fragheaderlen;
  1169. if ((flags & MSG_MORE) &&
  1170. !(rt->dst.dev->features&NETIF_F_SG))
  1171. alloclen = mtu;
  1172. else
  1173. alloclen = datalen + fragheaderlen;
  1174. /*
  1175. * The last fragment gets additional space at tail.
  1176. * Note: we overallocate on fragments with MSG_MODE
  1177. * because we have no idea if we're the last one.
  1178. */
  1179. if (datalen == length + fraggap)
  1180. alloclen += rt->dst.trailer_len;
  1181. /*
  1182. * We just reserve space for fragment header.
  1183. * Note: this may be overallocation if the message
  1184. * (without MSG_MORE) fits into the MTU.
  1185. */
  1186. alloclen += sizeof(struct frag_hdr);
  1187. if (transhdrlen) {
  1188. skb = sock_alloc_send_skb(sk,
  1189. alloclen + hh_len,
  1190. (flags & MSG_DONTWAIT), &err);
  1191. } else {
  1192. skb = NULL;
  1193. if (atomic_read(&sk->sk_wmem_alloc) <=
  1194. 2 * sk->sk_sndbuf)
  1195. skb = sock_wmalloc(sk,
  1196. alloclen + hh_len, 1,
  1197. sk->sk_allocation);
  1198. if (unlikely(skb == NULL))
  1199. err = -ENOBUFS;
  1200. else {
  1201. /* Only the initial fragment
  1202. * is time stamped.
  1203. */
  1204. tx_flags = 0;
  1205. }
  1206. }
  1207. if (skb == NULL)
  1208. goto error;
  1209. /*
  1210. * Fill in the control structures
  1211. */
  1212. skb->ip_summed = csummode;
  1213. skb->csum = 0;
  1214. /* reserve for fragmentation */
  1215. skb_reserve(skb, hh_len+sizeof(struct frag_hdr));
  1216. if (sk->sk_type == SOCK_DGRAM)
  1217. skb_shinfo(skb)->tx_flags = tx_flags;
  1218. /*
  1219. * Find where to start putting bytes
  1220. */
  1221. data = skb_put(skb, fraglen);
  1222. skb_set_network_header(skb, exthdrlen);
  1223. data += fragheaderlen;
  1224. skb->transport_header = (skb->network_header +
  1225. fragheaderlen);
  1226. if (fraggap) {
  1227. skb->csum = skb_copy_and_csum_bits(
  1228. skb_prev, maxfraglen,
  1229. data + transhdrlen, fraggap, 0);
  1230. skb_prev->csum = csum_sub(skb_prev->csum,
  1231. skb->csum);
  1232. data += fraggap;
  1233. pskb_trim_unique(skb_prev, maxfraglen);
  1234. }
  1235. copy = datalen - transhdrlen - fraggap;
  1236. if (copy < 0) {
  1237. err = -EINVAL;
  1238. kfree_skb(skb);
  1239. goto error;
  1240. } else if (copy > 0 && getfrag(from, data + transhdrlen, offset, copy, fraggap, skb) < 0) {
  1241. err = -EFAULT;
  1242. kfree_skb(skb);
  1243. goto error;
  1244. }
  1245. offset += copy;
  1246. length -= datalen - fraggap;
  1247. transhdrlen = 0;
  1248. exthdrlen = 0;
  1249. csummode = CHECKSUM_NONE;
  1250. /*
  1251. * Put the packet on the pending queue
  1252. */
  1253. __skb_queue_tail(&sk->sk_write_queue, skb);
  1254. continue;
  1255. }
  1256. if (copy > length)
  1257. copy = length;
  1258. if (!(rt->dst.dev->features&NETIF_F_SG)) {
  1259. unsigned int off;
  1260. off = skb->len;
  1261. if (getfrag(from, skb_put(skb, copy),
  1262. offset, copy, off, skb) < 0) {
  1263. __skb_trim(skb, off);
  1264. err = -EFAULT;
  1265. goto error;
  1266. }
  1267. } else {
  1268. int i = skb_shinfo(skb)->nr_frags;
  1269. skb_frag_t *frag = &skb_shinfo(skb)->frags[i-1];
  1270. struct page *page = sk->sk_sndmsg_page;
  1271. int off = sk->sk_sndmsg_off;
  1272. unsigned int left;
  1273. if (page && (left = PAGE_SIZE - off) > 0) {
  1274. if (copy >= left)
  1275. copy = left;
  1276. if (page != frag->page) {
  1277. if (i == MAX_SKB_FRAGS) {
  1278. err = -EMSGSIZE;
  1279. goto error;
  1280. }
  1281. get_page(page);
  1282. skb_fill_page_desc(skb, i, page, sk->sk_sndmsg_off, 0);
  1283. frag = &skb_shinfo(skb)->frags[i];
  1284. }
  1285. } else if(i < MAX_SKB_FRAGS) {
  1286. if (copy > PAGE_SIZE)
  1287. copy = PAGE_SIZE;
  1288. page = alloc_pages(sk->sk_allocation, 0);
  1289. if (page == NULL) {
  1290. err = -ENOMEM;
  1291. goto error;
  1292. }
  1293. sk->sk_sndmsg_page = page;
  1294. sk->sk_sndmsg_off = 0;
  1295. skb_fill_page_desc(skb, i, page, 0, 0);
  1296. frag = &skb_shinfo(skb)->frags[i];
  1297. } else {
  1298. err = -EMSGSIZE;
  1299. goto error;
  1300. }
  1301. if (getfrag(from, page_address(frag->page)+frag->page_offset+frag->size, offset, copy, skb->len, skb) < 0) {
  1302. err = -EFAULT;
  1303. goto error;
  1304. }
  1305. sk->sk_sndmsg_off += copy;
  1306. frag->size += copy;
  1307. skb->len += copy;
  1308. skb->data_len += copy;
  1309. skb->truesize += copy;
  1310. atomic_add(copy, &sk->sk_wmem_alloc);
  1311. }
  1312. offset += copy;
  1313. length -= copy;
  1314. }
  1315. return 0;
  1316. error:
  1317. cork->length -= length;
  1318. IP6_INC_STATS(sock_net(sk), rt->rt6i_idev, IPSTATS_MIB_OUTDISCARDS);
  1319. return err;
  1320. }
  1321. static void ip6_cork_release(struct inet_sock *inet, struct ipv6_pinfo *np)
  1322. {
  1323. if (np->cork.opt) {
  1324. kfree(np->cork.opt->dst0opt);
  1325. kfree(np->cork.opt->dst1opt);
  1326. kfree(np->cork.opt->hopopt);
  1327. kfree(np->cork.opt->srcrt);
  1328. kfree(np->cork.opt);
  1329. np->cork.opt = NULL;
  1330. }
  1331. if (inet->cork.base.dst) {
  1332. dst_release(inet->cork.base.dst);
  1333. inet->cork.base.dst = NULL;
  1334. inet->cork.base.flags &= ~IPCORK_ALLFRAG;
  1335. }
  1336. memset(&inet->cork.fl, 0, sizeof(inet->cork.fl));
  1337. }
  1338. int ip6_push_pending_frames(struct sock *sk)
  1339. {
  1340. struct sk_buff *skb, *tmp_skb;
  1341. struct sk_buff **tail_skb;
  1342. struct in6_addr final_dst_buf, *final_dst = &final_dst_buf;
  1343. struct inet_sock *inet = inet_sk(sk);
  1344. struct ipv6_pinfo *np = inet6_sk(sk);
  1345. struct net *net = sock_net(sk);
  1346. struct ipv6hdr *hdr;
  1347. struct ipv6_txoptions *opt = np->cork.opt;
  1348. struct rt6_info *rt = (struct rt6_info *)inet->cork.base.dst;
  1349. struct flowi6 *fl6 = &inet->cork.fl.u.ip6;
  1350. unsigned char proto = fl6->flowi6_proto;
  1351. int err = 0;
  1352. if ((skb = __skb_dequeue(&sk->sk_write_queue)) == NULL)
  1353. goto out;
  1354. tail_skb = &(skb_shinfo(skb)->frag_list);
  1355. /* move skb->data to ip header from ext header */
  1356. if (skb->data < skb_network_header(skb))
  1357. __skb_pull(skb, skb_network_offset(skb));
  1358. while ((tmp_skb = __skb_dequeue(&sk->sk_write_queue)) != NULL) {
  1359. __skb_pull(tmp_skb, skb_network_header_len(skb));
  1360. *tail_skb = tmp_skb;
  1361. tail_skb = &(tmp_skb->next);
  1362. skb->len += tmp_skb->len;
  1363. skb->data_len += tmp_skb->len;
  1364. skb->truesize += tmp_skb->truesize;
  1365. tmp_skb->destructor = NULL;
  1366. tmp_skb->sk = NULL;
  1367. }
  1368. /* Allow local fragmentation. */
  1369. if (np->pmtudisc < IPV6_PMTUDISC_DO)
  1370. skb->local_df = 1;
  1371. ipv6_addr_copy(final_dst, &fl6->daddr);
  1372. __skb_pull(skb, skb_network_header_len(skb));
  1373. if (opt && opt->opt_flen)
  1374. ipv6_push_frag_opts(skb, opt, &proto);
  1375. if (opt && opt->opt_nflen)
  1376. ipv6_push_nfrag_opts(skb, opt, &proto, &final_dst);
  1377. skb_push(skb, sizeof(struct ipv6hdr));
  1378. skb_reset_network_header(skb);
  1379. hdr = ipv6_hdr(skb);
  1380. *(__be32*)hdr = fl6->flowlabel |
  1381. htonl(0x60000000 | ((int)np->cork.tclass << 20));
  1382. hdr->hop_limit = np->cork.hop_limit;
  1383. hdr->nexthdr = proto;
  1384. ipv6_addr_copy(&hdr->saddr, &fl6->saddr);
  1385. ipv6_addr_copy(&hdr->daddr, final_dst);
  1386. skb->priority = sk->sk_priority;
  1387. skb->mark = sk->sk_mark;
  1388. skb_dst_set(skb, dst_clone(&rt->dst));
  1389. IP6_UPD_PO_STATS(net, rt->rt6i_idev, IPSTATS_MIB_OUT, skb->len);
  1390. if (proto == IPPROTO_ICMPV6) {
  1391. struct inet6_dev *idev = ip6_dst_idev(skb_dst(skb));
  1392. ICMP6MSGOUT_INC_STATS_BH(net, idev, icmp6_hdr(skb)->icmp6_type);
  1393. ICMP6_INC_STATS_BH(net, idev, ICMP6_MIB_OUTMSGS);
  1394. }
  1395. err = ip6_local_out(skb);
  1396. if (err) {
  1397. if (err > 0)
  1398. err = net_xmit_errno(err);
  1399. if (err)
  1400. goto error;
  1401. }
  1402. out:
  1403. ip6_cork_release(inet, np);
  1404. return err;
  1405. error:
  1406. IP6_INC_STATS(net, rt->rt6i_idev, IPSTATS_MIB_OUTDISCARDS);
  1407. goto out;
  1408. }
  1409. void ip6_flush_pending_frames(struct sock *sk)
  1410. {
  1411. struct sk_buff *skb;
  1412. while ((skb = __skb_dequeue_tail(&sk->sk_write_queue)) != NULL) {
  1413. if (skb_dst(skb))
  1414. IP6_INC_STATS(sock_net(sk), ip6_dst_idev(skb_dst(skb)),
  1415. IPSTATS_MIB_OUTDISCARDS);
  1416. kfree_skb(skb);
  1417. }
  1418. ip6_cork_release(inet_sk(sk), inet6_sk(sk));
  1419. }