reassembly.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  1. /*
  2. * IPv6 fragment reassembly
  3. * Linux INET6 implementation
  4. *
  5. * Authors:
  6. * Pedro Roque <roque@di.fc.ul.pt>
  7. *
  8. * Based on: net/ipv4/ip_fragment.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. /*
  16. * Fixes:
  17. * Andi Kleen Make it work with multiple hosts.
  18. * More RFC compliance.
  19. *
  20. * Horst von Brand Add missing #include <linux/string.h>
  21. * Alexey Kuznetsov SMP races, threading, cleanup.
  22. * Patrick McHardy LRU queue of frag heads for evictor.
  23. * Mitsuru KANDA @USAGI Register inet6_protocol{}.
  24. * David Stevens and
  25. * YOSHIFUJI,H. @USAGI Always remove fragment header to
  26. * calculate ICV correctly.
  27. */
  28. #define pr_fmt(fmt) "IPv6: " fmt
  29. #include <linux/errno.h>
  30. #include <linux/types.h>
  31. #include <linux/string.h>
  32. #include <linux/socket.h>
  33. #include <linux/sockios.h>
  34. #include <linux/jiffies.h>
  35. #include <linux/net.h>
  36. #include <linux/list.h>
  37. #include <linux/netdevice.h>
  38. #include <linux/in6.h>
  39. #include <linux/ipv6.h>
  40. #include <linux/icmpv6.h>
  41. #include <linux/random.h>
  42. #include <linux/jhash.h>
  43. #include <linux/skbuff.h>
  44. #include <linux/slab.h>
  45. #include <linux/export.h>
  46. #include <net/sock.h>
  47. #include <net/snmp.h>
  48. #include <net/ipv6.h>
  49. #include <net/ip6_route.h>
  50. #include <net/protocol.h>
  51. #include <net/transp_v6.h>
  52. #include <net/rawv6.h>
  53. #include <net/ndisc.h>
  54. #include <net/addrconf.h>
  55. #include <net/inet_frag.h>
  56. struct ip6frag_skb_cb
  57. {
  58. struct inet6_skb_parm h;
  59. int offset;
  60. };
  61. #define FRAG6_CB(skb) ((struct ip6frag_skb_cb*)((skb)->cb))
  62. /*
  63. * Equivalent of ipv4 struct ipq
  64. */
  65. struct frag_queue
  66. {
  67. struct inet_frag_queue q;
  68. __be32 id; /* fragment id */
  69. u32 user;
  70. struct in6_addr saddr;
  71. struct in6_addr daddr;
  72. int iif;
  73. unsigned int csum;
  74. __u16 nhoffset;
  75. };
  76. static struct inet_frags ip6_frags;
  77. int ip6_frag_nqueues(struct net *net)
  78. {
  79. return net->ipv6.frags.nqueues;
  80. }
  81. int ip6_frag_mem(struct net *net)
  82. {
  83. return atomic_read(&net->ipv6.frags.mem);
  84. }
  85. static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *prev,
  86. struct net_device *dev);
  87. /*
  88. * callers should be careful not to use the hash value outside the ipfrag_lock
  89. * as doing so could race with ipfrag_hash_rnd being recalculated.
  90. */
  91. unsigned int inet6_hash_frag(__be32 id, const struct in6_addr *saddr,
  92. const struct in6_addr *daddr, u32 rnd)
  93. {
  94. u32 c;
  95. c = jhash_3words((__force u32)saddr->s6_addr32[0],
  96. (__force u32)saddr->s6_addr32[1],
  97. (__force u32)saddr->s6_addr32[2],
  98. rnd);
  99. c = jhash_3words((__force u32)saddr->s6_addr32[3],
  100. (__force u32)daddr->s6_addr32[0],
  101. (__force u32)daddr->s6_addr32[1],
  102. c);
  103. c = jhash_3words((__force u32)daddr->s6_addr32[2],
  104. (__force u32)daddr->s6_addr32[3],
  105. (__force u32)id,
  106. c);
  107. return c & (INETFRAGS_HASHSZ - 1);
  108. }
  109. EXPORT_SYMBOL_GPL(inet6_hash_frag);
  110. static unsigned int ip6_hashfn(struct inet_frag_queue *q)
  111. {
  112. struct frag_queue *fq;
  113. fq = container_of(q, struct frag_queue, q);
  114. return inet6_hash_frag(fq->id, &fq->saddr, &fq->daddr, ip6_frags.rnd);
  115. }
  116. int ip6_frag_match(struct inet_frag_queue *q, void *a)
  117. {
  118. struct frag_queue *fq;
  119. struct ip6_create_arg *arg = a;
  120. fq = container_of(q, struct frag_queue, q);
  121. return (fq->id == arg->id && fq->user == arg->user &&
  122. ipv6_addr_equal(&fq->saddr, arg->src) &&
  123. ipv6_addr_equal(&fq->daddr, arg->dst));
  124. }
  125. EXPORT_SYMBOL(ip6_frag_match);
  126. void ip6_frag_init(struct inet_frag_queue *q, void *a)
  127. {
  128. struct frag_queue *fq = container_of(q, struct frag_queue, q);
  129. struct ip6_create_arg *arg = a;
  130. fq->id = arg->id;
  131. fq->user = arg->user;
  132. fq->saddr = *arg->src;
  133. fq->daddr = *arg->dst;
  134. }
  135. EXPORT_SYMBOL(ip6_frag_init);
  136. /* Destruction primitives. */
  137. static __inline__ void fq_put(struct frag_queue *fq)
  138. {
  139. inet_frag_put(&fq->q, &ip6_frags);
  140. }
  141. /* Kill fq entry. It is not destroyed immediately,
  142. * because caller (and someone more) holds reference count.
  143. */
  144. static __inline__ void fq_kill(struct frag_queue *fq)
  145. {
  146. inet_frag_kill(&fq->q, &ip6_frags);
  147. }
  148. static void ip6_evictor(struct net *net, struct inet6_dev *idev)
  149. {
  150. int evicted;
  151. evicted = inet_frag_evictor(&net->ipv6.frags, &ip6_frags);
  152. if (evicted)
  153. IP6_ADD_STATS_BH(net, idev, IPSTATS_MIB_REASMFAILS, evicted);
  154. }
  155. static void ip6_frag_expire(unsigned long data)
  156. {
  157. struct frag_queue *fq;
  158. struct net_device *dev = NULL;
  159. struct net *net;
  160. fq = container_of((struct inet_frag_queue *)data, struct frag_queue, q);
  161. spin_lock(&fq->q.lock);
  162. if (fq->q.last_in & INET_FRAG_COMPLETE)
  163. goto out;
  164. fq_kill(fq);
  165. net = container_of(fq->q.net, struct net, ipv6.frags);
  166. rcu_read_lock();
  167. dev = dev_get_by_index_rcu(net, fq->iif);
  168. if (!dev)
  169. goto out_rcu_unlock;
  170. IP6_INC_STATS_BH(net, __in6_dev_get(dev), IPSTATS_MIB_REASMTIMEOUT);
  171. IP6_INC_STATS_BH(net, __in6_dev_get(dev), IPSTATS_MIB_REASMFAILS);
  172. /* Don't send error if the first segment did not arrive. */
  173. if (!(fq->q.last_in & INET_FRAG_FIRST_IN) || !fq->q.fragments)
  174. goto out_rcu_unlock;
  175. /*
  176. But use as source device on which LAST ARRIVED
  177. segment was received. And do not use fq->dev
  178. pointer directly, device might already disappeared.
  179. */
  180. fq->q.fragments->dev = dev;
  181. icmpv6_send(fq->q.fragments, ICMPV6_TIME_EXCEED, ICMPV6_EXC_FRAGTIME, 0);
  182. out_rcu_unlock:
  183. rcu_read_unlock();
  184. out:
  185. spin_unlock(&fq->q.lock);
  186. fq_put(fq);
  187. }
  188. static __inline__ struct frag_queue *
  189. fq_find(struct net *net, __be32 id, const struct in6_addr *src, const struct in6_addr *dst)
  190. {
  191. struct inet_frag_queue *q;
  192. struct ip6_create_arg arg;
  193. unsigned int hash;
  194. arg.id = id;
  195. arg.user = IP6_DEFRAG_LOCAL_DELIVER;
  196. arg.src = src;
  197. arg.dst = dst;
  198. read_lock(&ip6_frags.lock);
  199. hash = inet6_hash_frag(id, src, dst, ip6_frags.rnd);
  200. q = inet_frag_find(&net->ipv6.frags, &ip6_frags, &arg, hash);
  201. if (IS_ERR_OR_NULL(q)) {
  202. inet_frag_maybe_warn_overflow(q, pr_fmt());
  203. return NULL;
  204. }
  205. return container_of(q, struct frag_queue, q);
  206. }
  207. static int ip6_frag_queue(struct frag_queue *fq, struct sk_buff *skb,
  208. struct frag_hdr *fhdr, int nhoff)
  209. {
  210. struct sk_buff *prev, *next;
  211. struct net_device *dev;
  212. int offset, end;
  213. struct net *net = dev_net(skb_dst(skb)->dev);
  214. if (fq->q.last_in & INET_FRAG_COMPLETE)
  215. goto err;
  216. offset = ntohs(fhdr->frag_off) & ~0x7;
  217. end = offset + (ntohs(ipv6_hdr(skb)->payload_len) -
  218. ((u8 *)(fhdr + 1) - (u8 *)(ipv6_hdr(skb) + 1)));
  219. if ((unsigned int)end > IPV6_MAXPLEN) {
  220. IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
  221. IPSTATS_MIB_INHDRERRORS);
  222. icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
  223. ((u8 *)&fhdr->frag_off -
  224. skb_network_header(skb)));
  225. return -1;
  226. }
  227. if (skb->ip_summed == CHECKSUM_COMPLETE) {
  228. const unsigned char *nh = skb_network_header(skb);
  229. skb->csum = csum_sub(skb->csum,
  230. csum_partial(nh, (u8 *)(fhdr + 1) - nh,
  231. 0));
  232. }
  233. /* Is this the final fragment? */
  234. if (!(fhdr->frag_off & htons(IP6_MF))) {
  235. /* If we already have some bits beyond end
  236. * or have different end, the segment is corrupted.
  237. */
  238. if (end < fq->q.len ||
  239. ((fq->q.last_in & INET_FRAG_LAST_IN) && end != fq->q.len))
  240. goto err;
  241. fq->q.last_in |= INET_FRAG_LAST_IN;
  242. fq->q.len = end;
  243. } else {
  244. /* Check if the fragment is rounded to 8 bytes.
  245. * Required by the RFC.
  246. */
  247. if (end & 0x7) {
  248. /* RFC2460 says always send parameter problem in
  249. * this case. -DaveM
  250. */
  251. IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
  252. IPSTATS_MIB_INHDRERRORS);
  253. icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
  254. offsetof(struct ipv6hdr, payload_len));
  255. return -1;
  256. }
  257. if (end > fq->q.len) {
  258. /* Some bits beyond end -> corruption. */
  259. if (fq->q.last_in & INET_FRAG_LAST_IN)
  260. goto err;
  261. fq->q.len = end;
  262. }
  263. }
  264. if (end == offset)
  265. goto err;
  266. /* Point into the IP datagram 'data' part. */
  267. if (!pskb_pull(skb, (u8 *) (fhdr + 1) - skb->data))
  268. goto err;
  269. if (pskb_trim_rcsum(skb, end - offset))
  270. goto err;
  271. /* Find out which fragments are in front and at the back of us
  272. * in the chain of fragments so far. We must know where to put
  273. * this fragment, right?
  274. */
  275. prev = fq->q.fragments_tail;
  276. if (!prev || FRAG6_CB(prev)->offset < offset) {
  277. next = NULL;
  278. goto found;
  279. }
  280. prev = NULL;
  281. for(next = fq->q.fragments; next != NULL; next = next->next) {
  282. if (FRAG6_CB(next)->offset >= offset)
  283. break; /* bingo! */
  284. prev = next;
  285. }
  286. found:
  287. /* RFC5722, Section 4, amended by Errata ID : 3089
  288. * When reassembling an IPv6 datagram, if
  289. * one or more its constituent fragments is determined to be an
  290. * overlapping fragment, the entire datagram (and any constituent
  291. * fragments) MUST be silently discarded.
  292. */
  293. /* Check for overlap with preceding fragment. */
  294. if (prev &&
  295. (FRAG6_CB(prev)->offset + prev->len) > offset)
  296. goto discard_fq;
  297. /* Look for overlap with succeeding segment. */
  298. if (next && FRAG6_CB(next)->offset < end)
  299. goto discard_fq;
  300. FRAG6_CB(skb)->offset = offset;
  301. /* Insert this fragment in the chain of fragments. */
  302. skb->next = next;
  303. if (!next)
  304. fq->q.fragments_tail = skb;
  305. if (prev)
  306. prev->next = skb;
  307. else
  308. fq->q.fragments = skb;
  309. dev = skb->dev;
  310. if (dev) {
  311. fq->iif = dev->ifindex;
  312. skb->dev = NULL;
  313. }
  314. fq->q.stamp = skb->tstamp;
  315. fq->q.meat += skb->len;
  316. atomic_add(skb->truesize, &fq->q.net->mem);
  317. /* The first fragment.
  318. * nhoffset is obtained from the first fragment, of course.
  319. */
  320. if (offset == 0) {
  321. fq->nhoffset = nhoff;
  322. fq->q.last_in |= INET_FRAG_FIRST_IN;
  323. }
  324. if (fq->q.last_in == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) &&
  325. fq->q.meat == fq->q.len) {
  326. int res;
  327. unsigned long orefdst = skb->_skb_refdst;
  328. skb->_skb_refdst = 0UL;
  329. res = ip6_frag_reasm(fq, prev, dev);
  330. skb->_skb_refdst = orefdst;
  331. return res;
  332. }
  333. skb_dst_drop(skb);
  334. write_lock(&ip6_frags.lock);
  335. list_move_tail(&fq->q.lru_list, &fq->q.net->lru_list);
  336. write_unlock(&ip6_frags.lock);
  337. return -1;
  338. discard_fq:
  339. fq_kill(fq);
  340. err:
  341. IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
  342. IPSTATS_MIB_REASMFAILS);
  343. kfree_skb(skb);
  344. return -1;
  345. }
  346. /*
  347. * Check if this packet is complete.
  348. * Returns NULL on failure by any reason, and pointer
  349. * to current nexthdr field in reassembled frame.
  350. *
  351. * It is called with locked fq, and caller must check that
  352. * queue is eligible for reassembly i.e. it is not COMPLETE,
  353. * the last and the first frames arrived and all the bits are here.
  354. */
  355. static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *prev,
  356. struct net_device *dev)
  357. {
  358. struct net *net = container_of(fq->q.net, struct net, ipv6.frags);
  359. struct sk_buff *fp, *head = fq->q.fragments;
  360. int payload_len;
  361. unsigned int nhoff;
  362. fq_kill(fq);
  363. /* Make the one we just received the head. */
  364. if (prev) {
  365. head = prev->next;
  366. fp = skb_clone(head, GFP_ATOMIC);
  367. if (!fp)
  368. goto out_oom;
  369. fp->next = head->next;
  370. if (!fp->next)
  371. fq->q.fragments_tail = fp;
  372. prev->next = fp;
  373. skb_morph(head, fq->q.fragments);
  374. head->next = fq->q.fragments->next;
  375. kfree_skb(fq->q.fragments);
  376. fq->q.fragments = head;
  377. }
  378. WARN_ON(head == NULL);
  379. WARN_ON(FRAG6_CB(head)->offset != 0);
  380. /* Unfragmented part is taken from the first segment. */
  381. payload_len = ((head->data - skb_network_header(head)) -
  382. sizeof(struct ipv6hdr) + fq->q.len -
  383. sizeof(struct frag_hdr));
  384. if (payload_len > IPV6_MAXPLEN)
  385. goto out_oversize;
  386. /* Head of list must not be cloned. */
  387. if (skb_cloned(head) && pskb_expand_head(head, 0, 0, GFP_ATOMIC))
  388. goto out_oom;
  389. /* If the first fragment is fragmented itself, we split
  390. * it to two chunks: the first with data and paged part
  391. * and the second, holding only fragments. */
  392. if (skb_has_frag_list(head)) {
  393. struct sk_buff *clone;
  394. int i, plen = 0;
  395. if ((clone = alloc_skb(0, GFP_ATOMIC)) == NULL)
  396. goto out_oom;
  397. clone->next = head->next;
  398. head->next = clone;
  399. skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
  400. skb_frag_list_init(head);
  401. for (i = 0; i < skb_shinfo(head)->nr_frags; i++)
  402. plen += skb_frag_size(&skb_shinfo(head)->frags[i]);
  403. clone->len = clone->data_len = head->data_len - plen;
  404. head->data_len -= clone->len;
  405. head->len -= clone->len;
  406. clone->csum = 0;
  407. clone->ip_summed = head->ip_summed;
  408. atomic_add(clone->truesize, &fq->q.net->mem);
  409. }
  410. /* We have to remove fragment header from datagram and to relocate
  411. * header in order to calculate ICV correctly. */
  412. nhoff = fq->nhoffset;
  413. skb_network_header(head)[nhoff] = skb_transport_header(head)[0];
  414. memmove(head->head + sizeof(struct frag_hdr), head->head,
  415. (head->data - head->head) - sizeof(struct frag_hdr));
  416. head->mac_header += sizeof(struct frag_hdr);
  417. head->network_header += sizeof(struct frag_hdr);
  418. skb_shinfo(head)->frag_list = head->next;
  419. skb_reset_transport_header(head);
  420. skb_push(head, head->data - skb_network_header(head));
  421. for (fp=head->next; fp; fp = fp->next) {
  422. head->data_len += fp->len;
  423. head->len += fp->len;
  424. if (head->ip_summed != fp->ip_summed)
  425. head->ip_summed = CHECKSUM_NONE;
  426. else if (head->ip_summed == CHECKSUM_COMPLETE)
  427. head->csum = csum_add(head->csum, fp->csum);
  428. head->truesize += fp->truesize;
  429. }
  430. atomic_sub(head->truesize, &fq->q.net->mem);
  431. head->next = NULL;
  432. head->dev = dev;
  433. head->tstamp = fq->q.stamp;
  434. ipv6_hdr(head)->payload_len = htons(payload_len);
  435. IP6CB(head)->nhoff = nhoff;
  436. IP6CB(head)->flags |= IP6SKB_FRAGMENTED;
  437. /* Yes, and fold redundant checksum back. 8) */
  438. if (head->ip_summed == CHECKSUM_COMPLETE)
  439. head->csum = csum_partial(skb_network_header(head),
  440. skb_network_header_len(head),
  441. head->csum);
  442. rcu_read_lock();
  443. IP6_INC_STATS_BH(net, __in6_dev_get(dev), IPSTATS_MIB_REASMOKS);
  444. rcu_read_unlock();
  445. fq->q.fragments = NULL;
  446. fq->q.fragments_tail = NULL;
  447. return 1;
  448. out_oversize:
  449. net_dbg_ratelimited("ip6_frag_reasm: payload len = %d\n", payload_len);
  450. goto out_fail;
  451. out_oom:
  452. net_dbg_ratelimited("ip6_frag_reasm: no memory for reassembly\n");
  453. out_fail:
  454. rcu_read_lock();
  455. IP6_INC_STATS_BH(net, __in6_dev_get(dev), IPSTATS_MIB_REASMFAILS);
  456. rcu_read_unlock();
  457. return -1;
  458. }
  459. static int ipv6_frag_rcv(struct sk_buff *skb)
  460. {
  461. struct frag_hdr *fhdr;
  462. struct frag_queue *fq;
  463. const struct ipv6hdr *hdr = ipv6_hdr(skb);
  464. struct net *net = dev_net(skb_dst(skb)->dev);
  465. if (IP6CB(skb)->flags & IP6SKB_FRAGMENTED)
  466. goto fail_hdr;
  467. IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMREQDS);
  468. /* Jumbo payload inhibits frag. header */
  469. if (hdr->payload_len==0)
  470. goto fail_hdr;
  471. if (!pskb_may_pull(skb, (skb_transport_offset(skb) +
  472. sizeof(struct frag_hdr))))
  473. goto fail_hdr;
  474. hdr = ipv6_hdr(skb);
  475. fhdr = (struct frag_hdr *)skb_transport_header(skb);
  476. if (!(fhdr->frag_off & htons(0xFFF9))) {
  477. /* It is not a fragmented frame */
  478. skb->transport_header += sizeof(struct frag_hdr);
  479. IP6_INC_STATS_BH(net,
  480. ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMOKS);
  481. IP6CB(skb)->nhoff = (u8 *)fhdr - skb_network_header(skb);
  482. IP6CB(skb)->flags |= IP6SKB_FRAGMENTED;
  483. return 1;
  484. }
  485. if (atomic_read(&net->ipv6.frags.mem) > net->ipv6.frags.high_thresh)
  486. ip6_evictor(net, ip6_dst_idev(skb_dst(skb)));
  487. fq = fq_find(net, fhdr->identification, &hdr->saddr, &hdr->daddr);
  488. if (fq != NULL) {
  489. int ret;
  490. spin_lock(&fq->q.lock);
  491. ret = ip6_frag_queue(fq, skb, fhdr, IP6CB(skb)->nhoff);
  492. spin_unlock(&fq->q.lock);
  493. fq_put(fq);
  494. return ret;
  495. }
  496. IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMFAILS);
  497. kfree_skb(skb);
  498. return -1;
  499. fail_hdr:
  500. IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_INHDRERRORS);
  501. icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, skb_network_header_len(skb));
  502. return -1;
  503. }
  504. static const struct inet6_protocol frag_protocol =
  505. {
  506. .handler = ipv6_frag_rcv,
  507. .flags = INET6_PROTO_NOPOLICY,
  508. };
  509. #ifdef CONFIG_SYSCTL
  510. static struct ctl_table ip6_frags_ns_ctl_table[] = {
  511. {
  512. .procname = "ip6frag_high_thresh",
  513. .data = &init_net.ipv6.frags.high_thresh,
  514. .maxlen = sizeof(int),
  515. .mode = 0644,
  516. .proc_handler = proc_dointvec
  517. },
  518. {
  519. .procname = "ip6frag_low_thresh",
  520. .data = &init_net.ipv6.frags.low_thresh,
  521. .maxlen = sizeof(int),
  522. .mode = 0644,
  523. .proc_handler = proc_dointvec
  524. },
  525. {
  526. .procname = "ip6frag_time",
  527. .data = &init_net.ipv6.frags.timeout,
  528. .maxlen = sizeof(int),
  529. .mode = 0644,
  530. .proc_handler = proc_dointvec_jiffies,
  531. },
  532. { }
  533. };
  534. static struct ctl_table ip6_frags_ctl_table[] = {
  535. {
  536. .procname = "ip6frag_secret_interval",
  537. .data = &ip6_frags.secret_interval,
  538. .maxlen = sizeof(int),
  539. .mode = 0644,
  540. .proc_handler = proc_dointvec_jiffies,
  541. },
  542. { }
  543. };
  544. static int __net_init ip6_frags_ns_sysctl_register(struct net *net)
  545. {
  546. struct ctl_table *table;
  547. struct ctl_table_header *hdr;
  548. table = ip6_frags_ns_ctl_table;
  549. if (!net_eq(net, &init_net)) {
  550. table = kmemdup(table, sizeof(ip6_frags_ns_ctl_table), GFP_KERNEL);
  551. if (table == NULL)
  552. goto err_alloc;
  553. table[0].data = &net->ipv6.frags.high_thresh;
  554. table[1].data = &net->ipv6.frags.low_thresh;
  555. table[2].data = &net->ipv6.frags.timeout;
  556. }
  557. hdr = register_net_sysctl_table(net, net_ipv6_ctl_path, table);
  558. if (hdr == NULL)
  559. goto err_reg;
  560. net->ipv6.sysctl.frags_hdr = hdr;
  561. return 0;
  562. err_reg:
  563. if (!net_eq(net, &init_net))
  564. kfree(table);
  565. err_alloc:
  566. return -ENOMEM;
  567. }
  568. static void __net_exit ip6_frags_ns_sysctl_unregister(struct net *net)
  569. {
  570. struct ctl_table *table;
  571. table = net->ipv6.sysctl.frags_hdr->ctl_table_arg;
  572. unregister_net_sysctl_table(net->ipv6.sysctl.frags_hdr);
  573. if (!net_eq(net, &init_net))
  574. kfree(table);
  575. }
  576. static struct ctl_table_header *ip6_ctl_header;
  577. static int ip6_frags_sysctl_register(void)
  578. {
  579. ip6_ctl_header = register_net_sysctl_rotable(net_ipv6_ctl_path,
  580. ip6_frags_ctl_table);
  581. return ip6_ctl_header == NULL ? -ENOMEM : 0;
  582. }
  583. static void ip6_frags_sysctl_unregister(void)
  584. {
  585. unregister_net_sysctl_table(ip6_ctl_header);
  586. }
  587. #else
  588. static inline int ip6_frags_ns_sysctl_register(struct net *net)
  589. {
  590. return 0;
  591. }
  592. static inline void ip6_frags_ns_sysctl_unregister(struct net *net)
  593. {
  594. }
  595. static inline int ip6_frags_sysctl_register(void)
  596. {
  597. return 0;
  598. }
  599. static inline void ip6_frags_sysctl_unregister(void)
  600. {
  601. }
  602. #endif
  603. static int __net_init ipv6_frags_init_net(struct net *net)
  604. {
  605. net->ipv6.frags.high_thresh = IPV6_FRAG_HIGH_THRESH;
  606. net->ipv6.frags.low_thresh = IPV6_FRAG_LOW_THRESH;
  607. net->ipv6.frags.timeout = IPV6_FRAG_TIMEOUT;
  608. inet_frags_init_net(&net->ipv6.frags);
  609. return ip6_frags_ns_sysctl_register(net);
  610. }
  611. static void __net_exit ipv6_frags_exit_net(struct net *net)
  612. {
  613. ip6_frags_ns_sysctl_unregister(net);
  614. inet_frags_exit_net(&net->ipv6.frags, &ip6_frags);
  615. }
  616. static struct pernet_operations ip6_frags_ops = {
  617. .init = ipv6_frags_init_net,
  618. .exit = ipv6_frags_exit_net,
  619. };
  620. int __init ipv6_frag_init(void)
  621. {
  622. int ret;
  623. ret = inet6_add_protocol(&frag_protocol, IPPROTO_FRAGMENT);
  624. if (ret)
  625. goto out;
  626. ret = ip6_frags_sysctl_register();
  627. if (ret)
  628. goto err_sysctl;
  629. ret = register_pernet_subsys(&ip6_frags_ops);
  630. if (ret)
  631. goto err_pernet;
  632. ip6_frags.hashfn = ip6_hashfn;
  633. ip6_frags.constructor = ip6_frag_init;
  634. ip6_frags.destructor = NULL;
  635. ip6_frags.skb_free = NULL;
  636. ip6_frags.qsize = sizeof(struct frag_queue);
  637. ip6_frags.match = ip6_frag_match;
  638. ip6_frags.frag_expire = ip6_frag_expire;
  639. ip6_frags.secret_interval = 10 * 60 * HZ;
  640. inet_frags_init(&ip6_frags);
  641. out:
  642. return ret;
  643. err_pernet:
  644. ip6_frags_sysctl_unregister();
  645. err_sysctl:
  646. inet6_del_protocol(&frag_protocol, IPPROTO_FRAGMENT);
  647. goto out;
  648. }
  649. void ipv6_frag_exit(void)
  650. {
  651. inet_frags_fini(&ip6_frags);
  652. ip6_frags_sysctl_unregister();
  653. unregister_pernet_subsys(&ip6_frags_ops);
  654. inet6_del_protocol(&frag_protocol, IPPROTO_FRAGMENT);
  655. }