ip_fragment.c 20 KB

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