ip_fragment.c 17 KB

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