arp_tables.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:32k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  * Packet matching code for ARP packets.
  3.  *
  4.  * Based heavily, if not almost entirely, upon ip_tables.c framework.
  5.  *
  6.  * Some ARP specific bits are:
  7.  *
  8.  * Copyright (C) 2002 David S. Miller (davem@redhat.com)
  9.  *
  10.  */
  11. #include <linux/config.h>
  12. #include <linux/kernel.h>
  13. #include <linux/skbuff.h>
  14. #include <linux/netdevice.h>
  15. #include <linux/if_arp.h>
  16. #include <linux/kmod.h>
  17. #include <linux/vmalloc.h>
  18. #include <linux/proc_fs.h>
  19. #include <linux/module.h>
  20. #include <linux/init.h>
  21. #include <asm/uaccess.h>
  22. #include <asm/semaphore.h>
  23. #include <linux/netfilter_arp/arp_tables.h>
  24. /*#define DEBUG_ARP_TABLES*/
  25. /*#define DEBUG_ARP_TABLES_USER*/
  26. #ifdef DEBUG_ARP_TABLES
  27. #define dprintf(format, args...)  printk(format , ## args)
  28. #else
  29. #define dprintf(format, args...)
  30. #endif
  31. #ifdef DEBUG_ARP_TABLES_USER
  32. #define duprintf(format, args...) printk(format , ## args)
  33. #else
  34. #define duprintf(format, args...)
  35. #endif
  36. #ifdef CONFIG_NETFILTER_DEBUG
  37. #define ARP_NF_ASSERT(x)
  38. do {
  39. if (!(x))
  40. printk("ARP_NF_ASSERT: %s:%s:%un",
  41.        __FUNCTION__, __FILE__, __LINE__);
  42. } while(0)
  43. #else
  44. #define ARP_NF_ASSERT(x)
  45. #endif
  46. #define SMP_ALIGN(x) (((x) + SMP_CACHE_BYTES-1) & ~(SMP_CACHE_BYTES-1))
  47. static DECLARE_MUTEX(arpt_mutex);
  48. #define ASSERT_READ_LOCK(x) ARP_NF_ASSERT(down_trylock(&arpt_mutex) != 0)
  49. #define ASSERT_WRITE_LOCK(x) ARP_NF_ASSERT(down_trylock(&arpt_mutex) != 0)
  50. #include <linux/netfilter_ipv4/lockhelp.h>
  51. #include <linux/netfilter_ipv4/listhelp.h>
  52. struct arpt_table_info {
  53. unsigned int size;
  54. unsigned int number;
  55. unsigned int initial_entries;
  56. unsigned int hook_entry[NF_ARP_NUMHOOKS];
  57. unsigned int underflow[NF_ARP_NUMHOOKS];
  58. char entries[0] __attribute__((aligned(SMP_CACHE_BYTES)));
  59. };
  60. static LIST_HEAD(arpt_target);
  61. static LIST_HEAD(arpt_tables);
  62. #define ADD_COUNTER(c,b,p) do { (c).bcnt += (b); (c).pcnt += (p); } while(0)
  63. #ifdef CONFIG_SMP
  64. #define TABLE_OFFSET(t,p) (SMP_ALIGN((t)->size)*(p))
  65. #else
  66. #define TABLE_OFFSET(t,p) 0
  67. #endif
  68. static inline int arp_devaddr_compare(const struct arpt_devaddr_info *ap,
  69.       char *hdr_addr, int len)
  70. {
  71. int i, ret;
  72. if (len > ARPT_DEV_ADDR_LEN_MAX)
  73. len = ARPT_DEV_ADDR_LEN_MAX;
  74. ret = 0;
  75. for (i = 0; i < len; i++)
  76. ret |= (hdr_addr[i] ^ ap->addr[i]) & ap->mask[i];
  77. return (ret != 0);
  78. }
  79. /* Returns whether packet matches rule or not. */
  80. static inline int arp_packet_match(const struct arphdr *arphdr,
  81.    struct net_device *dev,
  82.    const char *indev,
  83.    const char *outdev,
  84.    const struct arpt_arp *arpinfo)
  85. {
  86. char *arpptr = (char *)(arphdr + 1);
  87. char *src_devaddr, *tgt_devaddr;
  88. u32 *src_ipaddr, *tgt_ipaddr;
  89. int i, ret;
  90. #define FWINV(bool,invflg) ((bool) ^ !!(arpinfo->invflags & invflg))
  91. if (FWINV((arphdr->ar_op & arpinfo->arpop_mask) != arpinfo->arpop,
  92.   ARPT_INV_ARPOP)) {
  93. dprintf("ARP operation field mismatch.n");
  94. dprintf("ar_op: %04x info->arpop: %04x info->arpop_mask: %04xn",
  95. arphdr->ar_op, arpinfo->arpop, arpinfo->arpop_mask);
  96. return 0;
  97. }
  98. if (FWINV((arphdr->ar_hrd & arpinfo->arhrd_mask) != arpinfo->arhrd,
  99.   ARPT_INV_ARPHRD)) {
  100. dprintf("ARP hardware address format mismatch.n");
  101. dprintf("ar_hrd: %04x info->arhrd: %04x info->arhrd_mask: %04xn",
  102. arphdr->ar_hrd, arpinfo->arhrd, arpinfo->arhrd_mask);
  103. return 0;
  104. }
  105. if (FWINV((arphdr->ar_pro & arpinfo->arpro_mask) != arpinfo->arpro,
  106.   ARPT_INV_ARPPRO)) {
  107. dprintf("ARP protocol address format mismatch.n");
  108. dprintf("ar_pro: %04x info->arpro: %04x info->arpro_mask: %04xn",
  109. arphdr->ar_pro, arpinfo->arpro, arpinfo->arpro_mask);
  110. return 0;
  111. }
  112. if (FWINV((arphdr->ar_hln & arpinfo->arhln_mask) != arpinfo->arhln,
  113.   ARPT_INV_ARPHLN)) {
  114. dprintf("ARP hardware address length mismatch.n");
  115. dprintf("ar_hln: %02x info->arhln: %02x info->arhln_mask: %02xn",
  116. arphdr->ar_hln, arpinfo->arhln, arpinfo->arhln_mask);
  117. }
  118. src_devaddr = arpptr;
  119. arpptr += dev->addr_len;
  120. src_ipaddr = (u32 *) arpptr;
  121. arpptr += sizeof(u32);
  122. tgt_devaddr = arpptr;
  123. arpptr += dev->addr_len;
  124. tgt_ipaddr = (u32 *) arpptr;
  125. if (FWINV(arp_devaddr_compare(&arpinfo->src_devaddr, src_devaddr, dev->addr_len),
  126.   ARPT_INV_SRCDEVADDR) ||
  127.     FWINV(arp_devaddr_compare(&arpinfo->tgt_devaddr, tgt_devaddr, dev->addr_len),
  128.   ARPT_INV_TGTDEVADDR)) {
  129. dprintf("Source or target device address mismatch.n");
  130. return 0;
  131. }
  132. if (FWINV(((*src_ipaddr) & arpinfo->smsk.s_addr) != arpinfo->src.s_addr,
  133.   ARPT_INV_SRCIP) ||
  134.     FWINV((((*tgt_ipaddr) & arpinfo->tmsk.s_addr) != arpinfo->tgt.s_addr),
  135.   ARPT_INV_TGTIP)) {
  136. dprintf("Source or target IP address mismatch.n");
  137. dprintf("SRC: %u.%u.%u.%u. Mask: %u.%u.%u.%u. Target: %u.%u.%u.%u.%sn",
  138. NIPQUAD(*src_ipaddr),
  139. NIPQUAD(arpinfo->smsk.s_addr),
  140. NIPQUAD(arpinfo->src.s_addr),
  141. arpinfo->invflags & ARPT_INV_SRCIP ? " (INV)" : "");
  142. dprintf("TGT: %u.%u.%u.%u Mask: %u.%u.%u.%u Target: %u.%u.%u.%u.%sn",
  143. NIPQUAD(*tgt_ipaddr),
  144. NIPQUAD(arpinfo->tmsk.s_addr),
  145. NIPQUAD(arpinfo->tgt.s_addr),
  146. arpinfo->invflags & ARPT_INV_TGTIP ? " (INV)" : "");
  147. return 0;
  148. }
  149. /* Look for ifname matches; this should unroll nicely. */
  150. for (i = 0, ret = 0; i < IFNAMSIZ/sizeof(unsigned long); i++) {
  151. ret |= (((const unsigned long *)indev)[i]
  152. ^ ((const unsigned long *)arpinfo->iniface)[i])
  153. & ((const unsigned long *)arpinfo->iniface_mask)[i];
  154. }
  155. if (FWINV(ret != 0, ARPT_INV_VIA_IN)) {
  156. dprintf("VIA in mismatch (%s vs %s).%sn",
  157. indev, arpinfo->iniface,
  158. arpinfo->invflags&ARPT_INV_VIA_IN ?" (INV)":"");
  159. return 0;
  160. }
  161. for (i = 0, ret = 0; i < IFNAMSIZ/sizeof(unsigned long); i++) {
  162. ret |= (((const unsigned long *)outdev)[i]
  163. ^ ((const unsigned long *)arpinfo->outiface)[i])
  164. & ((const unsigned long *)arpinfo->outiface_mask)[i];
  165. }
  166. if (FWINV(ret != 0, ARPT_INV_VIA_OUT)) {
  167. dprintf("VIA out mismatch (%s vs %s).%sn",
  168. outdev, arpinfo->outiface,
  169. arpinfo->invflags&ARPT_INV_VIA_OUT ?" (INV)":"");
  170. return 0;
  171. }
  172. return 1;
  173. }
  174. static inline int arp_checkentry(const struct arpt_arp *arp)
  175. {
  176. if (arp->flags & ~ARPT_F_MASK) {
  177. duprintf("Unknown flag bits set: %08Xn",
  178.  arp->flags & ~ARPT_F_MASK);
  179. return 0;
  180. }
  181. if (arp->invflags & ~ARPT_INV_MASK) {
  182. duprintf("Unknown invflag bits set: %08Xn",
  183.  arp->invflags & ~ARPT_INV_MASK);
  184. return 0;
  185. }
  186. return 1;
  187. }
  188. static unsigned int arpt_error(struct sk_buff **pskb,
  189.        unsigned int hooknum,
  190.        const struct net_device *in,
  191.        const struct net_device *out,
  192.        const void *targinfo,
  193.        void *userinfo)
  194. {
  195. if (net_ratelimit())
  196. printk("arp_tables: error: '%s'n", (char *)targinfo);
  197. return NF_DROP;
  198. }
  199. static inline struct arpt_entry *get_entry(void *base, unsigned int offset)
  200. {
  201. return (struct arpt_entry *)(base + offset);
  202. }
  203. unsigned int arpt_do_table(struct sk_buff **pskb,
  204.    unsigned int hook,
  205.    const struct net_device *in,
  206.    const struct net_device *out,
  207.    struct arpt_table *table,
  208.    void *userdata)
  209. {
  210. static const char nulldevname[IFNAMSIZ] = { 0 };
  211. unsigned int verdict = NF_DROP;
  212. struct arphdr *arp = (*pskb)->nh.arph;
  213. int hotdrop = 0;
  214. struct arpt_entry *e, *back;
  215. const char *indev, *outdev;
  216. void *table_base;
  217. indev = in ? in->name : nulldevname;
  218. outdev = out ? out->name : nulldevname;
  219. read_lock_bh(&table->lock);
  220. table_base = (void *)table->private->entries
  221. + TABLE_OFFSET(table->private,
  222.        cpu_number_map(smp_processor_id()));
  223. e = get_entry(table_base, table->private->hook_entry[hook]);
  224. back = get_entry(table_base, table->private->underflow[hook]);
  225. do {
  226. if (arp_packet_match(arp, (*pskb)->dev, indev, outdev, &e->arp)) {
  227. struct arpt_entry_target *t;
  228. int hdr_len;
  229. hdr_len = sizeof(*arp) + (2 * sizeof(struct in_addr)) +
  230. (2 * (*pskb)->dev->addr_len);
  231. ADD_COUNTER(e->counters, hdr_len, 1);
  232. t = arpt_get_target(e);
  233. /* Standard target? */
  234. if (!t->u.kernel.target->target) {
  235. int v;
  236. v = ((struct arpt_standard_target *)t)->verdict;
  237. if (v < 0) {
  238. /* Pop from stack? */
  239. if (v != ARPT_RETURN) {
  240. verdict = (unsigned)(-v) - 1;
  241. break;
  242. }
  243. e = back;
  244. back = get_entry(table_base,
  245.  back->comefrom);
  246. continue;
  247. }
  248. if (table_base + v
  249.     != (void *)e + e->next_offset) {
  250. /* Save old back ptr in next entry */
  251. struct arpt_entry *next
  252. = (void *)e + e->next_offset;
  253. next->comefrom =
  254. (void *)back - table_base;
  255. /* set back pointer to next entry */
  256. back = next;
  257. }
  258. e = get_entry(table_base, v);
  259. } else {
  260. /* Targets which reenter must return
  261.  * abs. verdicts
  262.  */
  263. verdict = t->u.kernel.target->target(pskb,
  264.      hook,
  265.      in, out,
  266.      t->data,
  267.      userdata);
  268. /* Target might have changed stuff. */
  269. arp = (*pskb)->nh.arph;
  270. if (verdict == ARPT_CONTINUE)
  271. e = (void *)e + e->next_offset;
  272. else
  273. /* Verdict */
  274. break;
  275. }
  276. } else {
  277. e = (void *)e + e->next_offset;
  278. }
  279. } while (!hotdrop);
  280. read_unlock_bh(&table->lock);
  281. if (hotdrop)
  282. return NF_DROP;
  283. else
  284. return verdict;
  285. }
  286. static inline void *find_inlist_lock_noload(struct list_head *head,
  287.     const char *name,
  288.     int *error,
  289.     struct semaphore *mutex)
  290. {
  291. void *ret;
  292. *error = down_interruptible(mutex);
  293. if (*error != 0)
  294. return NULL;
  295. ret = list_named_find(head, name);
  296. if (!ret) {
  297. *error = -ENOENT;
  298. up(mutex);
  299. }
  300. return ret;
  301. }
  302. #ifndef CONFIG_KMOD
  303. #define find_inlist_lock(h,n,p,e,m) find_inlist_lock_noload((h),(n),(e),(m))
  304. #else
  305. static void *
  306. find_inlist_lock(struct list_head *head,
  307.  const char *name,
  308.  const char *prefix,
  309.  int *error,
  310.  struct semaphore *mutex)
  311. {
  312. void *ret;
  313. ret = find_inlist_lock_noload(head, name, error, mutex);
  314. if (!ret) {
  315. char modulename[ARPT_FUNCTION_MAXNAMELEN + strlen(prefix) + 1];
  316. strcpy(modulename, prefix);
  317. strcat(modulename, name);
  318. duprintf("find_inlist: loading `%s'.n", modulename);
  319. request_module(modulename);
  320. ret = find_inlist_lock_noload(head, name, error, mutex);
  321. }
  322. return ret;
  323. }
  324. #endif
  325. static inline struct arpt_table *find_table_lock(const char *name, int *error, struct semaphore *mutex)
  326. {
  327. return find_inlist_lock(&arpt_tables, name, "arptable_", error, mutex);
  328. }
  329. static inline struct arpt_target *find_target_lock(const char *name, int *error, struct semaphore *mutex)
  330. {
  331. return find_inlist_lock(&arpt_target, name, "arpt_", error, mutex);
  332. }
  333. /* All zeroes == unconditional rule. */
  334. static inline int unconditional(const struct arpt_arp *arp)
  335. {
  336. unsigned int i;
  337. for (i = 0; i < sizeof(*arp)/sizeof(__u32); i++)
  338. if (((__u32 *)arp)[i])
  339. return 0;
  340. return 1;
  341. }
  342. /* Figures out from what hook each rule can be called: returns 0 if
  343.  * there are loops.  Puts hook bitmask in comefrom.
  344.  */
  345. static int mark_source_chains(struct arpt_table_info *newinfo, unsigned int valid_hooks)
  346. {
  347. unsigned int hook;
  348. /* No recursion; use packet counter to save back ptrs (reset
  349.  * to 0 as we leave), and comefrom to save source hook bitmask.
  350.  */
  351. for (hook = 0; hook < NF_ARP_NUMHOOKS; hook++) {
  352. unsigned int pos = newinfo->hook_entry[hook];
  353. struct arpt_entry *e
  354. = (struct arpt_entry *)(newinfo->entries + pos);
  355. if (!(valid_hooks & (1 << hook)))
  356. continue;
  357. /* Set initial back pointer. */
  358. e->counters.pcnt = pos;
  359. for (;;) {
  360. struct arpt_standard_target *t
  361. = (void *)arpt_get_target(e);
  362. if (e->comefrom & (1 << NF_ARP_NUMHOOKS)) {
  363. printk("arptables: loop hook %u pos %u %08X.n",
  364.        hook, pos, e->comefrom);
  365. return 0;
  366. }
  367. e->comefrom
  368. |= ((1 << hook) | (1 << NF_ARP_NUMHOOKS));
  369. /* Unconditional return/END. */
  370. if (e->target_offset == sizeof(struct arpt_entry)
  371.     && (strcmp(t->target.u.user.name,
  372.        ARPT_STANDARD_TARGET) == 0)
  373.     && t->verdict < 0
  374.     && unconditional(&e->arp)) {
  375. unsigned int oldpos, size;
  376. /* Return: backtrack through the last
  377.  * big jump.
  378.  */
  379. do {
  380. e->comefrom ^= (1<<NF_ARP_NUMHOOKS);
  381. oldpos = pos;
  382. pos = e->counters.pcnt;
  383. e->counters.pcnt = 0;
  384. /* We're at the start. */
  385. if (pos == oldpos)
  386. goto next;
  387. e = (struct arpt_entry *)
  388. (newinfo->entries + pos);
  389. } while (oldpos == pos + e->next_offset);
  390. /* Move along one */
  391. size = e->next_offset;
  392. e = (struct arpt_entry *)
  393. (newinfo->entries + pos + size);
  394. e->counters.pcnt = pos;
  395. pos += size;
  396. } else {
  397. int newpos = t->verdict;
  398. if (strcmp(t->target.u.user.name,
  399.    ARPT_STANDARD_TARGET) == 0
  400.     && newpos >= 0) {
  401. /* This a jump; chase it. */
  402. duprintf("Jump rule %u -> %un",
  403.  pos, newpos);
  404. } else {
  405. /* ... this is a fallthru */
  406. newpos = pos + e->next_offset;
  407. }
  408. e = (struct arpt_entry *)
  409. (newinfo->entries + newpos);
  410. e->counters.pcnt = pos;
  411. pos = newpos;
  412. }
  413. }
  414. next:
  415. duprintf("Finished chain %un", hook);
  416. }
  417. return 1;
  418. }
  419. static inline int standard_check(const struct arpt_entry_target *t,
  420.  unsigned int max_offset)
  421. {
  422. struct arpt_standard_target *targ = (void *)t;
  423. /* Check standard info. */
  424. if (t->u.target_size
  425.     != ARPT_ALIGN(sizeof(struct arpt_standard_target))) {
  426. duprintf("arpt_standard_check: target size %u != %Zun",
  427.  t->u.target_size,
  428.  ARPT_ALIGN(sizeof(struct arpt_standard_target)));
  429. return 0;
  430. }
  431. if (targ->verdict >= 0
  432.     && targ->verdict > max_offset - sizeof(struct arpt_entry)) {
  433. duprintf("arpt_standard_check: bad verdict (%i)n",
  434.  targ->verdict);
  435. return 0;
  436. }
  437. if (targ->verdict < -NF_MAX_VERDICT - 1) {
  438. duprintf("arpt_standard_check: bad negative verdict (%i)n",
  439.  targ->verdict);
  440. return 0;
  441. }
  442. return 1;
  443. }
  444. static struct arpt_target arpt_standard_target;
  445. static inline int check_entry(struct arpt_entry *e, const char *name, unsigned int size,
  446.       unsigned int *i)
  447. {
  448. struct arpt_entry_target *t;
  449. struct arpt_target *target;
  450. int ret;
  451. if (!arp_checkentry(&e->arp)) {
  452. duprintf("arp_tables: arp check failed %p %s.n", e, name);
  453. return -EINVAL;
  454. }
  455. t = arpt_get_target(e);
  456. target = find_target_lock(t->u.user.name, &ret, &arpt_mutex);
  457. if (!target) {
  458. duprintf("check_entry: `%s' not foundn", t->u.user.name);
  459. goto out;
  460. }
  461. if (target->me)
  462. __MOD_INC_USE_COUNT(target->me);
  463. t->u.kernel.target = target;
  464. up(&arpt_mutex);
  465. if (t->u.kernel.target == &arpt_standard_target) {
  466. if (!standard_check(t, size)) {
  467. ret = -EINVAL;
  468. goto out;
  469. }
  470. } else if (t->u.kernel.target->checkentry
  471.    && !t->u.kernel.target->checkentry(name, e, t->data,
  472.       t->u.target_size
  473.       - sizeof(*t),
  474.       e->comefrom)) {
  475. if (t->u.kernel.target->me)
  476. __MOD_DEC_USE_COUNT(t->u.kernel.target->me);
  477. duprintf("arp_tables: check failed for `%s'.n",
  478.  t->u.kernel.target->name);
  479. ret = -EINVAL;
  480. goto out;
  481. }
  482. (*i)++;
  483. return 0;
  484. out:
  485. return ret;
  486. }
  487. static inline int check_entry_size_and_hooks(struct arpt_entry *e,
  488.      struct arpt_table_info *newinfo,
  489.      unsigned char *base,
  490.      unsigned char *limit,
  491.      const unsigned int *hook_entries,
  492.      const unsigned int *underflows,
  493.      unsigned int *i)
  494. {
  495. unsigned int h;
  496. if ((unsigned long)e % __alignof__(struct arpt_entry) != 0
  497.     || (unsigned char *)e + sizeof(struct arpt_entry) >= limit) {
  498. duprintf("Bad offset %pn", e);
  499. return -EINVAL;
  500. }
  501. if (e->next_offset
  502.     < sizeof(struct arpt_entry) + sizeof(struct arpt_entry_target)) {
  503. duprintf("checking: element %p size %un",
  504.  e, e->next_offset);
  505. return -EINVAL;
  506. }
  507. /* Check hooks & underflows */
  508. for (h = 0; h < NF_ARP_NUMHOOKS; h++) {
  509. if ((unsigned char *)e - base == hook_entries[h])
  510. newinfo->hook_entry[h] = hook_entries[h];
  511. if ((unsigned char *)e - base == underflows[h])
  512. newinfo->underflow[h] = underflows[h];
  513. }
  514. /* FIXME: underflows must be unconditional, standard verdicts
  515.            < 0 (not ARPT_RETURN). --RR */
  516. /* Clear counters and comefrom */
  517. e->counters = ((struct arpt_counters) { 0, 0 });
  518. e->comefrom = 0;
  519. (*i)++;
  520. return 0;
  521. }
  522. static inline int cleanup_entry(struct arpt_entry *e, unsigned int *i)
  523. {
  524. struct arpt_entry_target *t;
  525. if (i && (*i)-- == 0)
  526. return 1;
  527. t = arpt_get_target(e);
  528. if (t->u.kernel.target->destroy)
  529. t->u.kernel.target->destroy(t->data,
  530.     t->u.target_size - sizeof(*t));
  531. if (t->u.kernel.target->me)
  532. __MOD_DEC_USE_COUNT(t->u.kernel.target->me);
  533. return 0;
  534. }
  535. /* Checks and translates the user-supplied table segment (held in
  536.  * newinfo).
  537.  */
  538. static int translate_table(const char *name,
  539.    unsigned int valid_hooks,
  540.    struct arpt_table_info *newinfo,
  541.    unsigned int size,
  542.    unsigned int number,
  543.    const unsigned int *hook_entries,
  544.    const unsigned int *underflows)
  545. {
  546. unsigned int i;
  547. int ret;
  548. newinfo->size = size;
  549. newinfo->number = number;
  550. /* Init all hooks to impossible value. */
  551. for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
  552. newinfo->hook_entry[i] = 0xFFFFFFFF;
  553. newinfo->underflow[i] = 0xFFFFFFFF;
  554. }
  555. duprintf("translate_table: size %un", newinfo->size);
  556. i = 0;
  557. /* Walk through entries, checking offsets. */
  558. ret = ARPT_ENTRY_ITERATE(newinfo->entries, newinfo->size,
  559.  check_entry_size_and_hooks,
  560.  newinfo,
  561.  newinfo->entries,
  562.  newinfo->entries + size,
  563.  hook_entries, underflows, &i);
  564. duprintf("translate_table: ARPT_ENTRY_ITERATE gives %dn", ret);
  565. if (ret != 0)
  566. return ret;
  567. if (i != number) {
  568. duprintf("translate_table: %u not %u entriesn",
  569.  i, number);
  570. return -EINVAL;
  571. }
  572. /* Check hooks all assigned */
  573. for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
  574. /* Only hooks which are valid */
  575. if (!(valid_hooks & (1 << i)))
  576. continue;
  577. if (newinfo->hook_entry[i] == 0xFFFFFFFF) {
  578. duprintf("Invalid hook entry %u %un",
  579.  i, hook_entries[i]);
  580. return -EINVAL;
  581. }
  582. if (newinfo->underflow[i] == 0xFFFFFFFF) {
  583. duprintf("Invalid underflow %u %un",
  584.  i, underflows[i]);
  585. return -EINVAL;
  586. }
  587. }
  588. if (!mark_source_chains(newinfo, valid_hooks)) {
  589. duprintf("Looping hookn");
  590. return -ELOOP;
  591. }
  592. /* Finally, each sanity check must pass */
  593. i = 0;
  594. ret = ARPT_ENTRY_ITERATE(newinfo->entries, newinfo->size,
  595.  check_entry, name, size, &i);
  596. if (ret != 0) {
  597. ARPT_ENTRY_ITERATE(newinfo->entries, newinfo->size,
  598.    cleanup_entry, &i);
  599. return ret;
  600. }
  601. /* And one copy for every other CPU */
  602. for (i = 1; i < smp_num_cpus; i++) {
  603. memcpy(newinfo->entries + SMP_ALIGN(newinfo->size)*i,
  604.        newinfo->entries,
  605.        SMP_ALIGN(newinfo->size));
  606. }
  607. return ret;
  608. }
  609. static struct arpt_table_info *replace_table(struct arpt_table *table,
  610.      unsigned int num_counters,
  611.      struct arpt_table_info *newinfo,
  612.      int *error)
  613. {
  614. struct arpt_table_info *oldinfo;
  615. /* Do the substitution. */
  616. write_lock_bh(&table->lock);
  617. /* Check inside lock: is the old number correct? */
  618. if (num_counters != table->private->number) {
  619. duprintf("num_counters != table->private->number (%u/%u)n",
  620.  num_counters, table->private->number);
  621. write_unlock_bh(&table->lock);
  622. *error = -EAGAIN;
  623. return NULL;
  624. }
  625. oldinfo = table->private;
  626. table->private = newinfo;
  627. newinfo->initial_entries = oldinfo->initial_entries;
  628. write_unlock_bh(&table->lock);
  629. return oldinfo;
  630. }
  631. /* Gets counters. */
  632. static inline int add_entry_to_counter(const struct arpt_entry *e,
  633.        struct arpt_counters total[],
  634.        unsigned int *i)
  635. {
  636. ADD_COUNTER(total[*i], e->counters.bcnt, e->counters.pcnt);
  637. (*i)++;
  638. return 0;
  639. }
  640. static void get_counters(const struct arpt_table_info *t,
  641.  struct arpt_counters counters[])
  642. {
  643. unsigned int cpu;
  644. unsigned int i;
  645. for (cpu = 0; cpu < smp_num_cpus; cpu++) {
  646. i = 0;
  647. ARPT_ENTRY_ITERATE(t->entries + TABLE_OFFSET(t, cpu),
  648.    t->size,
  649.    add_entry_to_counter,
  650.    counters,
  651.    &i);
  652. }
  653. }
  654. static int copy_entries_to_user(unsigned int total_size,
  655. struct arpt_table *table,
  656. void *userptr)
  657. {
  658. unsigned int off, num, countersize;
  659. struct arpt_entry *e;
  660. struct arpt_counters *counters;
  661. int ret = 0;
  662. /* We need atomic snapshot of counters: rest doesn't change
  663.  * (other than comefrom, which userspace doesn't care
  664.  * about).
  665.  */
  666. countersize = sizeof(struct arpt_counters) * table->private->number;
  667. counters = vmalloc(countersize);
  668. if (counters == NULL)
  669. return -ENOMEM;
  670. /* First, sum counters... */
  671. memset(counters, 0, countersize);
  672. write_lock_bh(&table->lock);
  673. get_counters(table->private, counters);
  674. write_unlock_bh(&table->lock);
  675. /* ... then copy entire thing from CPU 0... */
  676. if (copy_to_user(userptr, table->private->entries, total_size) != 0) {
  677. ret = -EFAULT;
  678. goto free_counters;
  679. }
  680. /* FIXME: use iterator macros --RR */
  681. /* ... then go back and fix counters and names */
  682. for (off = 0, num = 0; off < total_size; off += e->next_offset, num++){
  683. struct arpt_entry_target *t;
  684. e = (struct arpt_entry *)(table->private->entries + off);
  685. if (copy_to_user(userptr + off
  686.  + offsetof(struct arpt_entry, counters),
  687.  &counters[num],
  688.  sizeof(counters[num])) != 0) {
  689. ret = -EFAULT;
  690. goto free_counters;
  691. }
  692. t = arpt_get_target(e);
  693. if (copy_to_user(userptr + off + e->target_offset
  694.  + offsetof(struct arpt_entry_target,
  695.     u.user.name),
  696.  t->u.kernel.target->name,
  697.  strlen(t->u.kernel.target->name)+1) != 0) {
  698. ret = -EFAULT;
  699. goto free_counters;
  700. }
  701. }
  702.  free_counters:
  703. vfree(counters);
  704. return ret;
  705. }
  706. static int get_entries(const struct arpt_get_entries *entries,
  707.        struct arpt_get_entries *uptr)
  708. {
  709. int ret;
  710. struct arpt_table *t;
  711. t = find_table_lock(entries->name, &ret, &arpt_mutex);
  712. if (t) {
  713. duprintf("t->private->number = %un",
  714.  t->private->number);
  715. if (entries->size == t->private->size)
  716. ret = copy_entries_to_user(t->private->size,
  717.    t, uptr->entrytable);
  718. else {
  719. duprintf("get_entries: I've got %u not %u!n",
  720.  t->private->size,
  721.  entries->size);
  722. ret = -EINVAL;
  723. }
  724. up(&arpt_mutex);
  725. } else
  726. duprintf("get_entries: Can't find %s!n",
  727.  entries->name);
  728. return ret;
  729. }
  730. static int do_replace(void *user, unsigned int len)
  731. {
  732. int ret;
  733. struct arpt_replace tmp;
  734. struct arpt_table *t;
  735. struct arpt_table_info *newinfo, *oldinfo;
  736. struct arpt_counters *counters;
  737. if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
  738. return -EFAULT;
  739. /* Hack: Causes ipchains to give correct error msg --RR */
  740. if (len != sizeof(tmp) + tmp.size)
  741. return -ENOPROTOOPT;
  742. /* Pedantry: prevent them from hitting BUG() in vmalloc.c --RR */
  743. if ((SMP_ALIGN(tmp.size) >> PAGE_SHIFT) + 2 > num_physpages)
  744. return -ENOMEM;
  745. newinfo = vmalloc(sizeof(struct arpt_table_info)
  746.   + SMP_ALIGN(tmp.size) * smp_num_cpus);
  747. if (!newinfo)
  748. return -ENOMEM;
  749. if (copy_from_user(newinfo->entries, user + sizeof(tmp),
  750.    tmp.size) != 0) {
  751. ret = -EFAULT;
  752. goto free_newinfo;
  753. }
  754. counters = vmalloc(tmp.num_counters * sizeof(struct arpt_counters));
  755. if (!counters) {
  756. ret = -ENOMEM;
  757. goto free_newinfo;
  758. }
  759. memset(counters, 0, tmp.num_counters * sizeof(struct arpt_counters));
  760. ret = translate_table(tmp.name, tmp.valid_hooks,
  761.       newinfo, tmp.size, tmp.num_entries,
  762.       tmp.hook_entry, tmp.underflow);
  763. if (ret != 0)
  764. goto free_newinfo_counters;
  765. duprintf("arp_tables: Translated tablen");
  766. t = find_table_lock(tmp.name, &ret, &arpt_mutex);
  767. if (!t)
  768. goto free_newinfo_counters_untrans;
  769. /* You lied! */
  770. if (tmp.valid_hooks != t->valid_hooks) {
  771. duprintf("Valid hook crap: %08X vs %08Xn",
  772.  tmp.valid_hooks, t->valid_hooks);
  773. ret = -EINVAL;
  774. goto free_newinfo_counters_untrans_unlock;
  775. }
  776. oldinfo = replace_table(t, tmp.num_counters, newinfo, &ret);
  777. if (!oldinfo)
  778. goto free_newinfo_counters_untrans_unlock;
  779. /* Update module usage count based on number of rules */
  780. duprintf("do_replace: oldnum=%u, initnum=%u, newnum=%un",
  781. oldinfo->number, oldinfo->initial_entries, newinfo->number);
  782. if (t->me && (oldinfo->number <= oldinfo->initial_entries) &&
  783.       (newinfo->number > oldinfo->initial_entries))
  784. __MOD_INC_USE_COUNT(t->me);
  785. else if (t->me && (oldinfo->number > oldinfo->initial_entries) &&
  786.    (newinfo->number <= oldinfo->initial_entries))
  787. __MOD_DEC_USE_COUNT(t->me);
  788. /* Get the old counters. */
  789. get_counters(oldinfo, counters);
  790. /* Decrease module usage counts and free resource */
  791. ARPT_ENTRY_ITERATE(oldinfo->entries, oldinfo->size, cleanup_entry,NULL);
  792. vfree(oldinfo);
  793. /* Silent error: too late now. */
  794. copy_to_user(tmp.counters, counters,
  795.      sizeof(struct arpt_counters) * tmp.num_counters);
  796. vfree(counters);
  797. up(&arpt_mutex);
  798. return 0;
  799.  free_newinfo_counters_untrans_unlock:
  800. up(&arpt_mutex);
  801.  free_newinfo_counters_untrans:
  802. ARPT_ENTRY_ITERATE(newinfo->entries, newinfo->size, cleanup_entry, NULL);
  803.  free_newinfo_counters:
  804. vfree(counters);
  805.  free_newinfo:
  806. vfree(newinfo);
  807. return ret;
  808. }
  809. /* We're lazy, and add to the first CPU; overflow works its fey magic
  810.  * and everything is OK.
  811.  */
  812. static inline int add_counter_to_entry(struct arpt_entry *e,
  813.        const struct arpt_counters addme[],
  814.        unsigned int *i)
  815. {
  816. ADD_COUNTER(e->counters, addme[*i].bcnt, addme[*i].pcnt);
  817. (*i)++;
  818. return 0;
  819. }
  820. static int do_add_counters(void *user, unsigned int len)
  821. {
  822. unsigned int i;
  823. struct arpt_counters_info tmp, *paddc;
  824. struct arpt_table *t;
  825. int ret;
  826. if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
  827. return -EFAULT;
  828. if (len != sizeof(tmp) + tmp.num_counters*sizeof(struct arpt_counters))
  829. return -EINVAL;
  830. paddc = vmalloc(len);
  831. if (!paddc)
  832. return -ENOMEM;
  833. if (copy_from_user(paddc, user, len) != 0) {
  834. ret = -EFAULT;
  835. goto free;
  836. }
  837. t = find_table_lock(tmp.name, &ret, &arpt_mutex);
  838. if (!t)
  839. goto free;
  840. write_lock_bh(&t->lock);
  841. if (t->private->number != paddc->num_counters) {
  842. ret = -EINVAL;
  843. goto unlock_up_free;
  844. }
  845. i = 0;
  846. ARPT_ENTRY_ITERATE(t->private->entries,
  847.    t->private->size,
  848.    add_counter_to_entry,
  849.    paddc->counters,
  850.    &i);
  851.  unlock_up_free:
  852. write_unlock_bh(&t->lock);
  853. up(&arpt_mutex);
  854.  free:
  855. vfree(paddc);
  856. return ret;
  857. }
  858. static int do_arpt_set_ctl(struct sock *sk, int cmd, void *user, unsigned int len)
  859. {
  860. int ret;
  861. if (!capable(CAP_NET_ADMIN))
  862. return -EPERM;
  863. switch (cmd) {
  864. case ARPT_SO_SET_REPLACE:
  865. ret = do_replace(user, len);
  866. break;
  867. case ARPT_SO_SET_ADD_COUNTERS:
  868. ret = do_add_counters(user, len);
  869. break;
  870. default:
  871. duprintf("do_arpt_set_ctl:  unknown request %in", cmd);
  872. ret = -EINVAL;
  873. }
  874. return ret;
  875. }
  876. static int do_arpt_get_ctl(struct sock *sk, int cmd, void *user, int *len)
  877. {
  878. int ret;
  879. if (!capable(CAP_NET_ADMIN))
  880. return -EPERM;
  881. switch (cmd) {
  882. case ARPT_SO_GET_INFO: {
  883. char name[ARPT_TABLE_MAXNAMELEN];
  884. struct arpt_table *t;
  885. if (*len != sizeof(struct arpt_getinfo)) {
  886. duprintf("length %u != %Zun", *len,
  887.  sizeof(struct arpt_getinfo));
  888. ret = -EINVAL;
  889. break;
  890. }
  891. if (copy_from_user(name, user, sizeof(name)) != 0) {
  892. ret = -EFAULT;
  893. break;
  894. }
  895. name[ARPT_TABLE_MAXNAMELEN-1] = '';
  896. t = find_table_lock(name, &ret, &arpt_mutex);
  897. if (t) {
  898. struct arpt_getinfo info;
  899. info.valid_hooks = t->valid_hooks;
  900. memcpy(info.hook_entry, t->private->hook_entry,
  901.        sizeof(info.hook_entry));
  902. memcpy(info.underflow, t->private->underflow,
  903.        sizeof(info.underflow));
  904. info.num_entries = t->private->number;
  905. info.size = t->private->size;
  906. strcpy(info.name, name);
  907. if (copy_to_user(user, &info, *len) != 0)
  908. ret = -EFAULT;
  909. else
  910. ret = 0;
  911. up(&arpt_mutex);
  912. }
  913. }
  914. break;
  915. case ARPT_SO_GET_ENTRIES: {
  916. struct arpt_get_entries get;
  917. if (*len < sizeof(get)) {
  918. duprintf("get_entries: %u < %Zun", *len, sizeof(get));
  919. ret = -EINVAL;
  920. } else if (copy_from_user(&get, user, sizeof(get)) != 0) {
  921. ret = -EFAULT;
  922. } else if (*len != sizeof(struct arpt_get_entries) + get.size) {
  923. duprintf("get_entries: %u != %Zun", *len,
  924.  sizeof(struct arpt_get_entries) + get.size);
  925. ret = -EINVAL;
  926. } else
  927. ret = get_entries(&get, user);
  928. break;
  929. }
  930. default:
  931. duprintf("do_arpt_get_ctl: unknown request %in", cmd);
  932. ret = -EINVAL;
  933. }
  934. return ret;
  935. }
  936. /* Registration hooks for targets. */
  937. int arpt_register_target(struct arpt_target *target)
  938. {
  939. int ret;
  940. MOD_INC_USE_COUNT;
  941. ret = down_interruptible(&arpt_mutex);
  942. if (ret != 0) {
  943. MOD_DEC_USE_COUNT;
  944. return ret;
  945. }
  946. if (!list_named_insert(&arpt_target, target)) {
  947. duprintf("arpt_register_target: `%s' already in list!n",
  948.  target->name);
  949. ret = -EINVAL;
  950. MOD_DEC_USE_COUNT;
  951. }
  952. up(&arpt_mutex);
  953. return ret;
  954. }
  955. void arpt_unregister_target(struct arpt_target *target)
  956. {
  957. down(&arpt_mutex);
  958. LIST_DELETE(&arpt_target, target);
  959. up(&arpt_mutex);
  960. MOD_DEC_USE_COUNT;
  961. }
  962. int arpt_register_table(struct arpt_table *table)
  963. {
  964. int ret;
  965. struct arpt_table_info *newinfo;
  966. static struct arpt_table_info bootstrap
  967. = { 0, 0, 0, { 0 }, { 0 }, { } };
  968. MOD_INC_USE_COUNT;
  969. newinfo = vmalloc(sizeof(struct arpt_table_info)
  970.   + SMP_ALIGN(table->table->size) * smp_num_cpus);
  971. if (!newinfo) {
  972. ret = -ENOMEM;
  973. MOD_DEC_USE_COUNT;
  974. return ret;
  975. }
  976. memcpy(newinfo->entries, table->table->entries, table->table->size);
  977. ret = translate_table(table->name, table->valid_hooks,
  978.       newinfo, table->table->size,
  979.       table->table->num_entries,
  980.       table->table->hook_entry,
  981.       table->table->underflow);
  982. duprintf("arpt_register_table: translate table gives %dn", ret);
  983. if (ret != 0) {
  984. vfree(newinfo);
  985. MOD_DEC_USE_COUNT;
  986. return ret;
  987. }
  988. ret = down_interruptible(&arpt_mutex);
  989. if (ret != 0) {
  990. vfree(newinfo);
  991. MOD_DEC_USE_COUNT;
  992. return ret;
  993. }
  994. /* Don't autoload: we'd eat our tail... */
  995. if (list_named_find(&arpt_tables, table->name)) {
  996. ret = -EEXIST;
  997. goto free_unlock;
  998. }
  999. /* Simplifies replace_table code. */
  1000. table->private = &bootstrap;
  1001. if (!replace_table(table, 0, newinfo, &ret))
  1002. goto free_unlock;
  1003. duprintf("table->private->number = %un",
  1004.  table->private->number);
  1005. /* save number of initial entries */
  1006. table->private->initial_entries = table->private->number;
  1007. table->lock = RW_LOCK_UNLOCKED;
  1008. list_prepend(&arpt_tables, table);
  1009.  unlock:
  1010. up(&arpt_mutex);
  1011. return ret;
  1012.  free_unlock:
  1013. vfree(newinfo);
  1014. MOD_DEC_USE_COUNT;
  1015. goto unlock;
  1016. }
  1017. void arpt_unregister_table(struct arpt_table *table)
  1018. {
  1019. down(&arpt_mutex);
  1020. LIST_DELETE(&arpt_tables, table);
  1021. up(&arpt_mutex);
  1022. /* Decrease module usage counts and free resources */
  1023. ARPT_ENTRY_ITERATE(table->private->entries, table->private->size,
  1024.    cleanup_entry, NULL);
  1025. vfree(table->private);
  1026. MOD_DEC_USE_COUNT;
  1027. }
  1028. /* The built-in targets: standard (NULL) and error. */
  1029. static struct arpt_target arpt_standard_target
  1030. = { { NULL, NULL }, ARPT_STANDARD_TARGET, NULL, NULL, NULL };
  1031. static struct arpt_target arpt_error_target
  1032. = { { NULL, NULL }, ARPT_ERROR_TARGET, arpt_error, NULL, NULL };
  1033. static struct nf_sockopt_ops arpt_sockopts
  1034. = { { NULL, NULL }, PF_INET, ARPT_BASE_CTL, ARPT_SO_SET_MAX+1, do_arpt_set_ctl,
  1035.     ARPT_BASE_CTL, ARPT_SO_GET_MAX+1, do_arpt_get_ctl, 0, NULL  };
  1036. #ifdef CONFIG_PROC_FS
  1037. static inline int print_name(const struct arpt_table *t,
  1038.      off_t start_offset, char *buffer, int length,
  1039.      off_t *pos, unsigned int *count)
  1040. {
  1041. if ((*count)++ >= start_offset) {
  1042. unsigned int namelen;
  1043. namelen = sprintf(buffer + *pos, "%sn", t->name);
  1044. if (*pos + namelen > length) {
  1045. /* Stop iterating */
  1046. return 1;
  1047. }
  1048. *pos += namelen;
  1049. }
  1050. return 0;
  1051. }
  1052. static int arpt_get_tables(char *buffer, char **start, off_t offset, int length)
  1053. {
  1054. off_t pos = 0;
  1055. unsigned int count = 0;
  1056. if (down_interruptible(&arpt_mutex) != 0)
  1057. return 0;
  1058. LIST_FIND(&arpt_tables, print_name, struct arpt_table *,
  1059.   offset, buffer, length, &pos, &count);
  1060. up(&arpt_mutex);
  1061. /* `start' hack - see fs/proc/generic.c line ~105 */
  1062. *start=(char *)((unsigned long)count-offset);
  1063. return pos;
  1064. }
  1065. #endif /*CONFIG_PROC_FS*/
  1066. static int __init init(void)
  1067. {
  1068. int ret;
  1069. /* Noone else will be downing sem now, so we won't sleep */
  1070. down(&arpt_mutex);
  1071. list_append(&arpt_target, &arpt_standard_target);
  1072. list_append(&arpt_target, &arpt_error_target);
  1073. up(&arpt_mutex);
  1074. /* Register setsockopt */
  1075. ret = nf_register_sockopt(&arpt_sockopts);
  1076. if (ret < 0) {
  1077. duprintf("Unable to register sockopts.n");
  1078. return ret;
  1079. }
  1080. #ifdef CONFIG_PROC_FS
  1081. {
  1082. struct proc_dir_entry *proc;
  1083. proc = proc_net_create("arp_tables_names", 0, arpt_get_tables);
  1084. if (!proc) {
  1085. nf_unregister_sockopt(&arpt_sockopts);
  1086. return -ENOMEM;
  1087. }
  1088. proc->owner = THIS_MODULE;
  1089. }
  1090. #endif
  1091. printk("arp_tables: (C) 2002 David S. Millern");
  1092. return 0;
  1093. }
  1094. static void __exit fini(void)
  1095. {
  1096. nf_unregister_sockopt(&arpt_sockopts);
  1097. #ifdef CONFIG_PROC_FS
  1098. proc_net_remove("arp_tables_names");
  1099. #endif
  1100. }
  1101. EXPORT_SYMBOL(arpt_register_table);
  1102. EXPORT_SYMBOL(arpt_unregister_table);
  1103. EXPORT_SYMBOL(arpt_do_table);
  1104. EXPORT_SYMBOL(arpt_register_target);
  1105. EXPORT_SYMBOL(arpt_unregister_target);
  1106. module_init(init);
  1107. module_exit(fini);
  1108. MODULE_LICENSE("GPL");