ip_fragment.c 18 KB

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