ip_fragment.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  1. /*
  2. * INET An implementation of the TCP/IP protocol suite for the LINUX
  3. * operating system. INET is implemented using the BSD Socket
  4. * interface as the means of communication with the user level.
  5. *
  6. * The IP fragmentation functionality.
  7. *
  8. * Authors: Fred N. van Kempen <waltje@uWalt.NL.Mugnet.ORG>
  9. * Alan Cox <alan@lxorguk.ukuu.org.uk>
  10. *
  11. * Fixes:
  12. * Alan Cox : Split from ip.c , see ip_input.c for history.
  13. * David S. Miller : Begin massive cleanup...
  14. * Andi Kleen : Add sysctls.
  15. * xxxx : Overlapfrag bug.
  16. * Ultima : ip_expire() kernel panic.
  17. * Bill Hawes : Frag accounting and evictor fixes.
  18. * John McDonald : 0 length frag bug.
  19. * Alexey Kuznetsov: SMP races, threading, cleanup.
  20. * Patrick McHardy : LRU queue of frag heads for evictor.
  21. */
  22. #define pr_fmt(fmt) "IPv4: " fmt
  23. #include <linux/compiler.h>
  24. #include <linux/module.h>
  25. #include <linux/types.h>
  26. #include <linux/mm.h>
  27. #include <linux/jiffies.h>
  28. #include <linux/skbuff.h>
  29. #include <linux/list.h>
  30. #include <linux/ip.h>
  31. #include <linux/icmp.h>
  32. #include <linux/netdevice.h>
  33. #include <linux/jhash.h>
  34. #include <linux/random.h>
  35. #include <linux/slab.h>
  36. #include <net/route.h>
  37. #include <net/dst.h>
  38. #include <net/sock.h>
  39. #include <net/ip.h>
  40. #include <net/icmp.h>
  41. #include <net/checksum.h>
  42. #include <net/inetpeer.h>
  43. #include <net/inet_frag.h>
  44. #include <linux/tcp.h>
  45. #include <linux/udp.h>
  46. #include <linux/inet.h>
  47. #include <linux/netfilter_ipv4.h>
  48. #include <net/inet_ecn.h>
  49. /* NOTE. Logic of IP defragmentation is parallel to corresponding IPv6
  50. * code now. If you change something here, _PLEASE_ update ipv6/reassembly.c
  51. * as well. Or notify me, at least. --ANK
  52. */
  53. static int sysctl_ipfrag_max_dist __read_mostly = 64;
  54. struct ipfrag_skb_cb
  55. {
  56. struct inet_skb_parm h;
  57. int offset;
  58. };
  59. #define FRAG_CB(skb) ((struct ipfrag_skb_cb *)((skb)->cb))
  60. /* Describe an entry in the "incomplete datagrams" queue. */
  61. struct ipq {
  62. struct inet_frag_queue q;
  63. u32 user;
  64. __be32 saddr;
  65. __be32 daddr;
  66. __be16 id;
  67. u8 protocol;
  68. u8 ecn; /* RFC3168 support */
  69. int iif;
  70. unsigned int rid;
  71. struct inet_peer *peer;
  72. };
  73. static inline u8 ip4_frag_ecn(u8 tos)
  74. {
  75. return 1 << (tos & INET_ECN_MASK);
  76. }
  77. static struct inet_frags ip4_frags;
  78. int ip_frag_mem(struct net *net)
  79. {
  80. return sum_frag_mem_limit(&net->ipv4.frags);
  81. }
  82. static int ip_frag_reasm(struct ipq *qp, struct sk_buff *prev,
  83. struct net_device *dev);
  84. struct ip4_create_arg {
  85. struct iphdr *iph;
  86. u32 user;
  87. };
  88. static unsigned int ipqhashfn(__be16 id, __be32 saddr, __be32 daddr, u8 prot)
  89. {
  90. net_get_random_once(&ip4_frags.rnd, sizeof(ip4_frags.rnd));
  91. return jhash_3words((__force u32)id << 16 | prot,
  92. (__force u32)saddr, (__force u32)daddr,
  93. ip4_frags.rnd);
  94. }
  95. static unsigned int ip4_hashfn(const struct inet_frag_queue *q)
  96. {
  97. const struct ipq *ipq;
  98. ipq = container_of(q, struct ipq, q);
  99. return ipqhashfn(ipq->id, ipq->saddr, ipq->daddr, ipq->protocol);
  100. }
  101. static bool ip4_frag_match(const struct inet_frag_queue *q, const void *a)
  102. {
  103. const struct ipq *qp;
  104. const struct ip4_create_arg *arg = a;
  105. qp = container_of(q, struct ipq, q);
  106. return qp->id == arg->iph->id &&
  107. qp->saddr == arg->iph->saddr &&
  108. qp->daddr == arg->iph->daddr &&
  109. qp->protocol == arg->iph->protocol &&
  110. qp->user == arg->user;
  111. }
  112. static void ip4_frag_init(struct inet_frag_queue *q, const void *a)
  113. {
  114. struct ipq *qp = container_of(q, struct ipq, q);
  115. struct netns_ipv4 *ipv4 = container_of(q->net, struct netns_ipv4,
  116. frags);
  117. struct net *net = container_of(ipv4, struct net, ipv4);
  118. const struct ip4_create_arg *arg = a;
  119. qp->protocol = arg->iph->protocol;
  120. qp->id = arg->iph->id;
  121. qp->ecn = ip4_frag_ecn(arg->iph->tos);
  122. qp->saddr = arg->iph->saddr;
  123. qp->daddr = arg->iph->daddr;
  124. qp->user = arg->user;
  125. qp->peer = sysctl_ipfrag_max_dist ?
  126. inet_getpeer_v4(net->ipv4.peers, arg->iph->saddr, 1) : NULL;
  127. }
  128. static __inline__ void ip4_frag_free(struct inet_frag_queue *q)
  129. {
  130. struct ipq *qp;
  131. qp = container_of(q, struct ipq, q);
  132. if (qp->peer)
  133. inet_putpeer(qp->peer);
  134. }
  135. /* Destruction primitives. */
  136. static __inline__ void ipq_put(struct ipq *ipq)
  137. {
  138. inet_frag_put(&ipq->q, &ip4_frags);
  139. }
  140. /* Kill ipq entry. It is not destroyed immediately,
  141. * because caller (and someone more) holds reference count.
  142. */
  143. static void ipq_kill(struct ipq *ipq)
  144. {
  145. inet_frag_kill(&ipq->q, &ip4_frags);
  146. }
  147. /*
  148. * Oops, a fragment queue timed out. Kill it and send an ICMP reply.
  149. */
  150. static void ip_expire(unsigned long arg)
  151. {
  152. struct ipq *qp;
  153. struct net *net;
  154. qp = container_of((struct inet_frag_queue *) arg, struct ipq, q);
  155. net = container_of(qp->q.net, struct net, ipv4.frags);
  156. spin_lock(&qp->q.lock);
  157. if (qp->q.last_in & INET_FRAG_COMPLETE)
  158. goto out;
  159. ipq_kill(qp);
  160. if (!(qp->q.last_in & INET_FRAG_EVICTED))
  161. IP_INC_STATS_BH(net, IPSTATS_MIB_REASMTIMEOUT);
  162. IP_INC_STATS_BH(net, IPSTATS_MIB_REASMFAILS);
  163. if ((qp->q.last_in & INET_FRAG_FIRST_IN) && qp->q.fragments != NULL) {
  164. struct sk_buff *head = qp->q.fragments;
  165. const struct iphdr *iph;
  166. int err;
  167. rcu_read_lock();
  168. head->dev = dev_get_by_index_rcu(net, qp->iif);
  169. if (!head->dev)
  170. goto out_rcu_unlock;
  171. /* skb has no dst, perform route lookup again */
  172. iph = ip_hdr(head);
  173. err = ip_route_input_noref(head, iph->daddr, iph->saddr,
  174. iph->tos, head->dev);
  175. if (err)
  176. goto out_rcu_unlock;
  177. /*
  178. * Only an end host needs to send an ICMP
  179. * "Fragment Reassembly Timeout" message, per RFC792.
  180. */
  181. if (qp->user == IP_DEFRAG_AF_PACKET ||
  182. ((qp->user >= IP_DEFRAG_CONNTRACK_IN) &&
  183. (qp->user <= __IP_DEFRAG_CONNTRACK_IN_END) &&
  184. (skb_rtable(head)->rt_type != RTN_LOCAL)))
  185. goto out_rcu_unlock;
  186. /* Send an ICMP "Fragment Reassembly Timeout" message. */
  187. icmp_send(head, ICMP_TIME_EXCEEDED, ICMP_EXC_FRAGTIME, 0);
  188. out_rcu_unlock:
  189. rcu_read_unlock();
  190. }
  191. out:
  192. spin_unlock(&qp->q.lock);
  193. ipq_put(qp);
  194. }
  195. /* Find the correct entry in the "incomplete datagrams" queue for
  196. * this IP datagram, and create new one, if nothing is found.
  197. */
  198. static inline struct ipq *ip_find(struct net *net, struct iphdr *iph, u32 user)
  199. {
  200. struct inet_frag_queue *q;
  201. struct ip4_create_arg arg;
  202. unsigned int hash;
  203. arg.iph = iph;
  204. arg.user = user;
  205. read_lock(&ip4_frags.lock);
  206. hash = ipqhashfn(iph->id, iph->saddr, iph->daddr, iph->protocol);
  207. q = inet_frag_find(&net->ipv4.frags, &ip4_frags, &arg, hash);
  208. if (IS_ERR_OR_NULL(q)) {
  209. inet_frag_maybe_warn_overflow(q, pr_fmt());
  210. return NULL;
  211. }
  212. return container_of(q, struct ipq, q);
  213. }
  214. /* Is the fragment too far ahead to be part of ipq? */
  215. static inline int ip_frag_too_far(struct ipq *qp)
  216. {
  217. struct inet_peer *peer = qp->peer;
  218. unsigned int max = sysctl_ipfrag_max_dist;
  219. unsigned int start, end;
  220. int rc;
  221. if (!peer || !max)
  222. return 0;
  223. start = qp->rid;
  224. end = atomic_inc_return(&peer->rid);
  225. qp->rid = end;
  226. rc = qp->q.fragments && (end - start) > max;
  227. if (rc) {
  228. struct net *net;
  229. net = container_of(qp->q.net, struct net, ipv4.frags);
  230. IP_INC_STATS_BH(net, IPSTATS_MIB_REASMFAILS);
  231. }
  232. return rc;
  233. }
  234. static int ip_frag_reinit(struct ipq *qp)
  235. {
  236. struct sk_buff *fp;
  237. unsigned int sum_truesize = 0;
  238. if (!mod_timer(&qp->q.timer, jiffies + qp->q.net->timeout)) {
  239. atomic_inc(&qp->q.refcnt);
  240. return -ETIMEDOUT;
  241. }
  242. fp = qp->q.fragments;
  243. do {
  244. struct sk_buff *xp = fp->next;
  245. sum_truesize += fp->truesize;
  246. kfree_skb(fp);
  247. fp = xp;
  248. } while (fp);
  249. sub_frag_mem_limit(&qp->q, sum_truesize);
  250. qp->q.last_in = 0;
  251. qp->q.len = 0;
  252. qp->q.meat = 0;
  253. qp->q.fragments = NULL;
  254. qp->q.fragments_tail = NULL;
  255. qp->iif = 0;
  256. qp->ecn = 0;
  257. return 0;
  258. }
  259. /* Add new segment to existing queue. */
  260. static int ip_frag_queue(struct ipq *qp, struct sk_buff *skb)
  261. {
  262. struct sk_buff *prev, *next;
  263. struct net_device *dev;
  264. int flags, offset;
  265. int ihl, end;
  266. int err = -ENOENT;
  267. u8 ecn;
  268. if (qp->q.last_in & INET_FRAG_COMPLETE)
  269. goto err;
  270. if (!(IPCB(skb)->flags & IPSKB_FRAG_COMPLETE) &&
  271. unlikely(ip_frag_too_far(qp)) &&
  272. unlikely(err = ip_frag_reinit(qp))) {
  273. ipq_kill(qp);
  274. goto err;
  275. }
  276. ecn = ip4_frag_ecn(ip_hdr(skb)->tos);
  277. offset = ntohs(ip_hdr(skb)->frag_off);
  278. flags = offset & ~IP_OFFSET;
  279. offset &= IP_OFFSET;
  280. offset <<= 3; /* offset is in 8-byte chunks */
  281. ihl = ip_hdrlen(skb);
  282. /* Determine the position of this fragment. */
  283. end = offset + skb->len - ihl;
  284. err = -EINVAL;
  285. /* Is this the final fragment? */
  286. if ((flags & IP_MF) == 0) {
  287. /* If we already have some bits beyond end
  288. * or have different end, the segment is corrupted.
  289. */
  290. if (end < qp->q.len ||
  291. ((qp->q.last_in & INET_FRAG_LAST_IN) && end != qp->q.len))
  292. goto err;
  293. qp->q.last_in |= INET_FRAG_LAST_IN;
  294. qp->q.len = end;
  295. } else {
  296. if (end&7) {
  297. end &= ~7;
  298. if (skb->ip_summed != CHECKSUM_UNNECESSARY)
  299. skb->ip_summed = CHECKSUM_NONE;
  300. }
  301. if (end > qp->q.len) {
  302. /* Some bits beyond end -> corruption. */
  303. if (qp->q.last_in & INET_FRAG_LAST_IN)
  304. goto err;
  305. qp->q.len = end;
  306. }
  307. }
  308. if (end == offset)
  309. goto err;
  310. err = -ENOMEM;
  311. if (pskb_pull(skb, ihl) == NULL)
  312. goto err;
  313. err = pskb_trim_rcsum(skb, end - offset);
  314. if (err)
  315. goto err;
  316. /* Find out which fragments are in front and at the back of us
  317. * in the chain of fragments so far. We must know where to put
  318. * this fragment, right?
  319. */
  320. prev = qp->q.fragments_tail;
  321. if (!prev || FRAG_CB(prev)->offset < offset) {
  322. next = NULL;
  323. goto found;
  324. }
  325. prev = NULL;
  326. for (next = qp->q.fragments; next != NULL; next = next->next) {
  327. if (FRAG_CB(next)->offset >= offset)
  328. break; /* bingo! */
  329. prev = next;
  330. }
  331. found:
  332. /* We found where to put this one. Check for overlap with
  333. * preceding fragment, and, if needed, align things so that
  334. * any overlaps are eliminated.
  335. */
  336. if (prev) {
  337. int i = (FRAG_CB(prev)->offset + prev->len) - offset;
  338. if (i > 0) {
  339. offset += i;
  340. err = -EINVAL;
  341. if (end <= offset)
  342. goto err;
  343. err = -ENOMEM;
  344. if (!pskb_pull(skb, i))
  345. goto err;
  346. if (skb->ip_summed != CHECKSUM_UNNECESSARY)
  347. skb->ip_summed = CHECKSUM_NONE;
  348. }
  349. }
  350. err = -ENOMEM;
  351. while (next && FRAG_CB(next)->offset < end) {
  352. int i = end - FRAG_CB(next)->offset; /* overlap is 'i' bytes */
  353. if (i < next->len) {
  354. /* Eat head of the next overlapped fragment
  355. * and leave the loop. The next ones cannot overlap.
  356. */
  357. if (!pskb_pull(next, i))
  358. goto err;
  359. FRAG_CB(next)->offset += i;
  360. qp->q.meat -= i;
  361. if (next->ip_summed != CHECKSUM_UNNECESSARY)
  362. next->ip_summed = CHECKSUM_NONE;
  363. break;
  364. } else {
  365. struct sk_buff *free_it = next;
  366. /* Old fragment is completely overridden with
  367. * new one drop it.
  368. */
  369. next = next->next;
  370. if (prev)
  371. prev->next = next;
  372. else
  373. qp->q.fragments = next;
  374. qp->q.meat -= free_it->len;
  375. sub_frag_mem_limit(&qp->q, free_it->truesize);
  376. kfree_skb(free_it);
  377. }
  378. }
  379. FRAG_CB(skb)->offset = offset;
  380. /* Insert this fragment in the chain of fragments. */
  381. skb->next = next;
  382. if (!next)
  383. qp->q.fragments_tail = skb;
  384. if (prev)
  385. prev->next = skb;
  386. else
  387. qp->q.fragments = skb;
  388. dev = skb->dev;
  389. if (dev) {
  390. qp->iif = dev->ifindex;
  391. skb->dev = NULL;
  392. }
  393. qp->q.stamp = skb->tstamp;
  394. qp->q.meat += skb->len;
  395. qp->ecn |= ecn;
  396. add_frag_mem_limit(&qp->q, skb->truesize);
  397. if (offset == 0)
  398. qp->q.last_in |= INET_FRAG_FIRST_IN;
  399. if (ip_hdr(skb)->frag_off & htons(IP_DF) &&
  400. skb->len + ihl > qp->q.max_size)
  401. qp->q.max_size = skb->len + ihl;
  402. if (qp->q.last_in == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) &&
  403. qp->q.meat == qp->q.len) {
  404. unsigned long orefdst = skb->_skb_refdst;
  405. skb->_skb_refdst = 0UL;
  406. err = ip_frag_reasm(qp, prev, dev);
  407. skb->_skb_refdst = orefdst;
  408. return err;
  409. }
  410. skb_dst_drop(skb);
  411. return -EINPROGRESS;
  412. err:
  413. kfree_skb(skb);
  414. return err;
  415. }
  416. /* Build a new IP datagram from all its fragments. */
  417. static int ip_frag_reasm(struct ipq *qp, struct sk_buff *prev,
  418. struct net_device *dev)
  419. {
  420. struct net *net = container_of(qp->q.net, struct net, ipv4.frags);
  421. struct iphdr *iph;
  422. struct sk_buff *fp, *head = qp->q.fragments;
  423. int len;
  424. int ihlen;
  425. int err;
  426. int sum_truesize;
  427. u8 ecn;
  428. ipq_kill(qp);
  429. ecn = ip_frag_ecn_table[qp->ecn];
  430. if (unlikely(ecn == 0xff)) {
  431. err = -EINVAL;
  432. goto out_fail;
  433. }
  434. /* Make the one we just received the head. */
  435. if (prev) {
  436. head = prev->next;
  437. fp = skb_clone(head, GFP_ATOMIC);
  438. if (!fp)
  439. goto out_nomem;
  440. fp->next = head->next;
  441. if (!fp->next)
  442. qp->q.fragments_tail = fp;
  443. prev->next = fp;
  444. skb_morph(head, qp->q.fragments);
  445. head->next = qp->q.fragments->next;
  446. consume_skb(qp->q.fragments);
  447. qp->q.fragments = head;
  448. }
  449. WARN_ON(head == NULL);
  450. WARN_ON(FRAG_CB(head)->offset != 0);
  451. /* Allocate a new buffer for the datagram. */
  452. ihlen = ip_hdrlen(head);
  453. len = ihlen + qp->q.len;
  454. err = -E2BIG;
  455. if (len > 65535)
  456. goto out_oversize;
  457. /* Head of list must not be cloned. */
  458. if (skb_unclone(head, GFP_ATOMIC))
  459. goto out_nomem;
  460. /* If the first fragment is fragmented itself, we split
  461. * it to two chunks: the first with data and paged part
  462. * and the second, holding only fragments. */
  463. if (skb_has_frag_list(head)) {
  464. struct sk_buff *clone;
  465. int i, plen = 0;
  466. if ((clone = alloc_skb(0, GFP_ATOMIC)) == NULL)
  467. goto out_nomem;
  468. clone->next = head->next;
  469. head->next = clone;
  470. skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
  471. skb_frag_list_init(head);
  472. for (i = 0; i < skb_shinfo(head)->nr_frags; i++)
  473. plen += skb_frag_size(&skb_shinfo(head)->frags[i]);
  474. clone->len = clone->data_len = head->data_len - plen;
  475. head->data_len -= clone->len;
  476. head->len -= clone->len;
  477. clone->csum = 0;
  478. clone->ip_summed = head->ip_summed;
  479. add_frag_mem_limit(&qp->q, clone->truesize);
  480. }
  481. skb_push(head, head->data - skb_network_header(head));
  482. sum_truesize = head->truesize;
  483. for (fp = head->next; fp;) {
  484. bool headstolen;
  485. int delta;
  486. struct sk_buff *next = fp->next;
  487. sum_truesize += fp->truesize;
  488. if (head->ip_summed != fp->ip_summed)
  489. head->ip_summed = CHECKSUM_NONE;
  490. else if (head->ip_summed == CHECKSUM_COMPLETE)
  491. head->csum = csum_add(head->csum, fp->csum);
  492. if (skb_try_coalesce(head, fp, &headstolen, &delta)) {
  493. kfree_skb_partial(fp, headstolen);
  494. } else {
  495. if (!skb_shinfo(head)->frag_list)
  496. skb_shinfo(head)->frag_list = fp;
  497. head->data_len += fp->len;
  498. head->len += fp->len;
  499. head->truesize += fp->truesize;
  500. }
  501. fp = next;
  502. }
  503. sub_frag_mem_limit(&qp->q, sum_truesize);
  504. head->next = NULL;
  505. head->dev = dev;
  506. head->tstamp = qp->q.stamp;
  507. IPCB(head)->frag_max_size = qp->q.max_size;
  508. iph = ip_hdr(head);
  509. /* max_size != 0 implies at least one fragment had IP_DF set */
  510. iph->frag_off = qp->q.max_size ? htons(IP_DF) : 0;
  511. iph->tot_len = htons(len);
  512. iph->tos |= ecn;
  513. IP_INC_STATS_BH(net, IPSTATS_MIB_REASMOKS);
  514. qp->q.fragments = NULL;
  515. qp->q.fragments_tail = NULL;
  516. return 0;
  517. out_nomem:
  518. LIMIT_NETDEBUG(KERN_ERR pr_fmt("queue_glue: no memory for gluing queue %p\n"),
  519. qp);
  520. err = -ENOMEM;
  521. goto out_fail;
  522. out_oversize:
  523. net_info_ratelimited("Oversized IP packet from %pI4\n", &qp->saddr);
  524. out_fail:
  525. IP_INC_STATS_BH(net, IPSTATS_MIB_REASMFAILS);
  526. return err;
  527. }
  528. /* Process an incoming IP datagram fragment. */
  529. int ip_defrag(struct sk_buff *skb, u32 user)
  530. {
  531. struct ipq *qp;
  532. struct net *net;
  533. net = skb->dev ? dev_net(skb->dev) : dev_net(skb_dst(skb)->dev);
  534. IP_INC_STATS_BH(net, IPSTATS_MIB_REASMREQDS);
  535. /* Lookup (or create) queue header */
  536. if ((qp = ip_find(net, ip_hdr(skb), user)) != NULL) {
  537. int ret;
  538. spin_lock(&qp->q.lock);
  539. ret = ip_frag_queue(qp, skb);
  540. spin_unlock(&qp->q.lock);
  541. ipq_put(qp);
  542. return ret;
  543. }
  544. IP_INC_STATS_BH(net, IPSTATS_MIB_REASMFAILS);
  545. kfree_skb(skb);
  546. return -ENOMEM;
  547. }
  548. EXPORT_SYMBOL(ip_defrag);
  549. struct sk_buff *ip_check_defrag(struct sk_buff *skb, u32 user)
  550. {
  551. struct iphdr iph;
  552. u32 len;
  553. if (skb->protocol != htons(ETH_P_IP))
  554. return skb;
  555. if (!skb_copy_bits(skb, 0, &iph, sizeof(iph)))
  556. return skb;
  557. if (iph.ihl < 5 || iph.version != 4)
  558. return skb;
  559. len = ntohs(iph.tot_len);
  560. if (skb->len < len || len < (iph.ihl * 4))
  561. return skb;
  562. if (ip_is_fragment(&iph)) {
  563. skb = skb_share_check(skb, GFP_ATOMIC);
  564. if (skb) {
  565. if (!pskb_may_pull(skb, iph.ihl*4))
  566. return skb;
  567. if (pskb_trim_rcsum(skb, len))
  568. return skb;
  569. memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
  570. if (ip_defrag(skb, user))
  571. return NULL;
  572. skb_clear_hash(skb);
  573. }
  574. }
  575. return skb;
  576. }
  577. EXPORT_SYMBOL(ip_check_defrag);
  578. #ifdef CONFIG_SYSCTL
  579. static int zero;
  580. static struct ctl_table ip4_frags_ns_ctl_table[] = {
  581. {
  582. .procname = "ipfrag_high_thresh",
  583. .data = &init_net.ipv4.frags.high_thresh,
  584. .maxlen = sizeof(int),
  585. .mode = 0644,
  586. .proc_handler = proc_dointvec
  587. },
  588. {
  589. .procname = "ipfrag_low_thresh",
  590. .data = &init_net.ipv4.frags.low_thresh,
  591. .maxlen = sizeof(int),
  592. .mode = 0644,
  593. .proc_handler = proc_dointvec
  594. },
  595. {
  596. .procname = "ipfrag_time",
  597. .data = &init_net.ipv4.frags.timeout,
  598. .maxlen = sizeof(int),
  599. .mode = 0644,
  600. .proc_handler = proc_dointvec_jiffies,
  601. },
  602. { }
  603. };
  604. /* secret interval has been deprecated */
  605. static int ip4_frags_secret_interval_unused;
  606. static struct ctl_table ip4_frags_ctl_table[] = {
  607. {
  608. .procname = "ipfrag_secret_interval",
  609. .data = &ip4_frags_secret_interval_unused,
  610. .maxlen = sizeof(int),
  611. .mode = 0644,
  612. .proc_handler = proc_dointvec_jiffies,
  613. },
  614. {
  615. .procname = "ipfrag_max_dist",
  616. .data = &sysctl_ipfrag_max_dist,
  617. .maxlen = sizeof(int),
  618. .mode = 0644,
  619. .proc_handler = proc_dointvec_minmax,
  620. .extra1 = &zero
  621. },
  622. { }
  623. };
  624. static int __net_init ip4_frags_ns_ctl_register(struct net *net)
  625. {
  626. struct ctl_table *table;
  627. struct ctl_table_header *hdr;
  628. table = ip4_frags_ns_ctl_table;
  629. if (!net_eq(net, &init_net)) {
  630. table = kmemdup(table, sizeof(ip4_frags_ns_ctl_table), GFP_KERNEL);
  631. if (table == NULL)
  632. goto err_alloc;
  633. table[0].data = &net->ipv4.frags.high_thresh;
  634. table[1].data = &net->ipv4.frags.low_thresh;
  635. table[2].data = &net->ipv4.frags.timeout;
  636. /* Don't export sysctls to unprivileged users */
  637. if (net->user_ns != &init_user_ns)
  638. table[0].procname = NULL;
  639. }
  640. hdr = register_net_sysctl(net, "net/ipv4", table);
  641. if (hdr == NULL)
  642. goto err_reg;
  643. net->ipv4.frags_hdr = hdr;
  644. return 0;
  645. err_reg:
  646. if (!net_eq(net, &init_net))
  647. kfree(table);
  648. err_alloc:
  649. return -ENOMEM;
  650. }
  651. static void __net_exit ip4_frags_ns_ctl_unregister(struct net *net)
  652. {
  653. struct ctl_table *table;
  654. table = net->ipv4.frags_hdr->ctl_table_arg;
  655. unregister_net_sysctl_table(net->ipv4.frags_hdr);
  656. kfree(table);
  657. }
  658. static void ip4_frags_ctl_register(void)
  659. {
  660. register_net_sysctl(&init_net, "net/ipv4", ip4_frags_ctl_table);
  661. }
  662. #else
  663. static inline int ip4_frags_ns_ctl_register(struct net *net)
  664. {
  665. return 0;
  666. }
  667. static inline void ip4_frags_ns_ctl_unregister(struct net *net)
  668. {
  669. }
  670. static inline void ip4_frags_ctl_register(void)
  671. {
  672. }
  673. #endif
  674. static int __net_init ipv4_frags_init_net(struct net *net)
  675. {
  676. /* Fragment cache limits.
  677. *
  678. * The fragment memory accounting code, (tries to) account for
  679. * the real memory usage, by measuring both the size of frag
  680. * queue struct (inet_frag_queue (ipv4:ipq/ipv6:frag_queue))
  681. * and the SKB's truesize.
  682. *
  683. * A 64K fragment consumes 129736 bytes (44*2944)+200
  684. * (1500 truesize == 2944, sizeof(struct ipq) == 200)
  685. *
  686. * We will commit 4MB at one time. Should we cross that limit
  687. * we will prune down to 3MB, making room for approx 8 big 64K
  688. * fragments 8x128k.
  689. */
  690. net->ipv4.frags.high_thresh = 4 * 1024 * 1024;
  691. net->ipv4.frags.low_thresh = 3 * 1024 * 1024;
  692. /*
  693. * Important NOTE! Fragment queue must be destroyed before MSL expires.
  694. * RFC791 is wrong proposing to prolongate timer each fragment arrival
  695. * by TTL.
  696. */
  697. net->ipv4.frags.timeout = IP_FRAG_TIME;
  698. inet_frags_init_net(&net->ipv4.frags);
  699. return ip4_frags_ns_ctl_register(net);
  700. }
  701. static void __net_exit ipv4_frags_exit_net(struct net *net)
  702. {
  703. ip4_frags_ns_ctl_unregister(net);
  704. inet_frags_exit_net(&net->ipv4.frags, &ip4_frags);
  705. }
  706. static struct pernet_operations ip4_frags_ops = {
  707. .init = ipv4_frags_init_net,
  708. .exit = ipv4_frags_exit_net,
  709. };
  710. void __init ipfrag_init(void)
  711. {
  712. ip4_frags_ctl_register();
  713. register_pernet_subsys(&ip4_frags_ops);
  714. ip4_frags.hashfn = ip4_hashfn;
  715. ip4_frags.constructor = ip4_frag_init;
  716. ip4_frags.destructor = ip4_frag_free;
  717. ip4_frags.skb_free = NULL;
  718. ip4_frags.qsize = sizeof(struct ipq);
  719. ip4_frags.match = ip4_frag_match;
  720. ip4_frags.frag_expire = ip_expire;
  721. inet_frags_init(&ip4_frags);
  722. }