ip_fragment.c 20 KB

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