ip_fragment.c 20 KB

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