ip_fragment.c 17 KB

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