ip_fragment.c 17 KB

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