ip6_output.c 42 KB

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