1 /* 2 * net/sched/pedit.c Generic packet editor 3 * 4 * This program is free software; you can redistribute it and/or 5 * modify it under the terms of the GNU General Public License 6 * as published by the Free Software Foundation; either version 7 * 2 of the License, or (at your option) any later version. 8 * 9 * Authors: Jamal Hadi Salim (2002-4) 10 */ 11 12 #include <asm/uaccess.h> 13 #include <asm/system.h> 14 #include <asm/bitops.h> 15 #include <linux/types.h> 16 #include <linux/kernel.h> 17 #include <linux/sched.h> 18 #include <linux/string.h> 19 #include <linux/mm.h> 20 #include <linux/socket.h> 21 #include <linux/sockios.h> 22 #include <linux/in.h> 23 #include <linux/errno.h> 24 #include <linux/interrupt.h> 25 #include <linux/netdevice.h> 26 #include <linux/skbuff.h> 27 #include <linux/rtnetlink.h> 28 #include <linux/module.h> 29 #include <linux/init.h> 30 #include <linux/proc_fs.h> 31 #include <net/sock.h> 32 #include <net/pkt_sched.h> 33 #include <linux/tc_act/tc_pedit.h> 34 #include <net/tc_act/tc_pedit.h> 35 36 37 #define PEDIT_DEB 1 38 39 /* use generic hash table */ 40 #define MY_TAB_SIZE 16 41 #define MY_TAB_MASK 15 42 static u32 idx_gen; 43 static struct tcf_pedit *tcf_pedit_ht[MY_TAB_SIZE]; 44 static DEFINE_RWLOCK(pedit_lock); 45 46 #define tcf_st tcf_pedit 47 #define tc_st tc_pedit 48 #define tcf_t_lock pedit_lock 49 #define tcf_ht tcf_pedit_ht 50 51 #define CONFIG_NET_ACT_INIT 1 52 #include <net/pkt_act.h> 53 54 static int 55 tcf_pedit_init(struct rtattr *rta, struct rtattr *est, struct tc_action *a, 56 int ovr, int bind) 57 { 58 struct rtattr *tb[TCA_PEDIT_MAX]; 59 struct tc_pedit *parm; 60 int ret = 0; 61 struct tcf_pedit *p; 62 struct tc_pedit_key *keys = NULL; 63 int ksize; 64 65 if (rta == NULL || rtattr_parse_nested(tb, TCA_PEDIT_MAX, rta) < 0) 66 return -EINVAL; 67 68 if (tb[TCA_PEDIT_PARMS - 1] == NULL || 69 RTA_PAYLOAD(tb[TCA_PEDIT_PARMS-1]) < sizeof(*parm)) 70 return -EINVAL; 71 parm = RTA_DATA(tb[TCA_PEDIT_PARMS-1]); 72 ksize = parm->nkeys * sizeof(struct tc_pedit_key); 73 if (RTA_PAYLOAD(tb[TCA_PEDIT_PARMS-1]) < sizeof(*parm) + ksize) 74 return -EINVAL; 75 76 p = tcf_hash_check(parm->index, a, ovr, bind); 77 if (p == NULL) { 78 if (!parm->nkeys) 79 return -EINVAL; 80 p = tcf_hash_create(parm->index, est, a, sizeof(*p), ovr, bind); 81 if (p == NULL) 82 return -ENOMEM; 83 keys = kmalloc(ksize, GFP_KERNEL); 84 if (keys == NULL) { 85 kfree(p); 86 return -ENOMEM; 87 } 88 ret = ACT_P_CREATED; 89 } else { 90 if (!ovr) { 91 tcf_hash_release(p, bind); 92 return -EEXIST; 93 } 94 if (p->nkeys && p->nkeys != parm->nkeys) { 95 keys = kmalloc(ksize, GFP_KERNEL); 96 if (keys == NULL) 97 return -ENOMEM; 98 } 99 } 100 101 spin_lock_bh(&p->lock); 102 p->flags = parm->flags; 103 p->action = parm->action; 104 if (keys) { 105 kfree(p->keys); 106 p->keys = keys; 107 p->nkeys = parm->nkeys; 108 } 109 memcpy(p->keys, parm->keys, ksize); 110 spin_unlock_bh(&p->lock); 111 if (ret == ACT_P_CREATED) 112 tcf_hash_insert(p); 113 return ret; 114 } 115 116 static int 117 tcf_pedit_cleanup(struct tc_action *a, int bind) 118 { 119 struct tcf_pedit *p = PRIV(a, pedit); 120 121 if (p != NULL) { 122 struct tc_pedit_key *keys = p->keys; 123 if (tcf_hash_release(p, bind)) { 124 kfree(keys); 125 return 1; 126 } 127 } 128 return 0; 129 } 130 131 static int 132 tcf_pedit(struct sk_buff *skb, struct tc_action *a, struct tcf_result *res) 133 { 134 struct tcf_pedit *p = PRIV(a, pedit); 135 int i, munged = 0; 136 u8 *pptr; 137 138 if (!(skb->tc_verd & TC_OK2MUNGE)) { 139 /* should we set skb->cloned? */ 140 if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) { 141 return p->action; 142 } 143 } 144 145 pptr = skb->nh.raw; 146 147 spin_lock(&p->lock); 148 149 p->tm.lastuse = jiffies; 150 151 if (p->nkeys > 0) { 152 struct tc_pedit_key *tkey = p->keys; 153 154 for (i = p->nkeys; i > 0; i--, tkey++) { 155 u32 *ptr; 156 int offset = tkey->off; 157 158 if (tkey->offmask) { 159 if (skb->len > tkey->at) { 160 char *j = pptr + tkey->at; 161 offset += ((*j & tkey->offmask) >> 162 tkey->shift); 163 } else { 164 goto bad; 165 } 166 } 167 168 if (offset % 4) { 169 printk("offset must be on 32 bit boundaries\n"); 170 goto bad; 171 } 172 if (skb->len < 0 || (offset > 0 && offset > skb->len)) { 173 printk("offset %d cant exceed pkt length %d\n", 174 offset, skb->len); 175 goto bad; 176 } 177 178 ptr = (u32 *)(pptr+offset); 179 /* just do it, baby */ 180 *ptr = ((*ptr & tkey->mask) ^ tkey->val); 181 munged++; 182 } 183 184 if (munged) 185 skb->tc_verd = SET_TC_MUNGED(skb->tc_verd); 186 goto done; 187 } else { 188 printk("pedit BUG: index %d\n",p->index); 189 } 190 191 bad: 192 p->qstats.overlimits++; 193 done: 194 p->bstats.bytes += skb->len; 195 p->bstats.packets++; 196 spin_unlock(&p->lock); 197 return p->action; 198 } 199 200 static int 201 tcf_pedit_dump(struct sk_buff *skb, struct tc_action *a,int bind, int ref) 202 { 203 unsigned char *b = skb->tail; 204 struct tc_pedit *opt; 205 struct tcf_pedit *p = PRIV(a, pedit); 206 struct tcf_t t; 207 int s; 208 209 s = sizeof(*opt) + p->nkeys * sizeof(struct tc_pedit_key); 210 211 /* netlink spinlocks held above us - must use ATOMIC */ 212 opt = kmalloc(s, GFP_ATOMIC); 213 if (opt == NULL) 214 return -ENOBUFS; 215 memset(opt, 0, s); 216 217 memcpy(opt->keys, p->keys, p->nkeys * sizeof(struct tc_pedit_key)); 218 opt->index = p->index; 219 opt->nkeys = p->nkeys; 220 opt->flags = p->flags; 221 opt->action = p->action; 222 opt->refcnt = p->refcnt - ref; 223 opt->bindcnt = p->bindcnt - bind; 224 225 226 #ifdef PEDIT_DEB 227 { 228 /* Debug - get rid of later */ 229 int i; 230 struct tc_pedit_key *key = opt->keys; 231 232 for (i=0; i<opt->nkeys; i++, key++) { 233 printk( "\n key #%d",i); 234 printk( " at %d: val %08x mask %08x", 235 (unsigned int)key->off, 236 (unsigned int)key->val, 237 (unsigned int)key->mask); 238 } 239 } 240 #endif 241 242 RTA_PUT(skb, TCA_PEDIT_PARMS, s, opt); 243 t.install = jiffies_to_clock_t(jiffies - p->tm.install); 244 t.lastuse = jiffies_to_clock_t(jiffies - p->tm.lastuse); 245 t.expires = jiffies_to_clock_t(p->tm.expires); 246 RTA_PUT(skb, TCA_PEDIT_TM, sizeof(t), &t); 247 kfree(opt); 248 return skb->len; 249 250 rtattr_failure: 251 skb_trim(skb, b - skb->data); 252 kfree(opt); 253 return -1; 254 } 255 256 static 257 struct tc_action_ops act_pedit_ops = { 258 .kind = "pedit", 259 .type = TCA_ACT_PEDIT, 260 .capab = TCA_CAP_NONE, 261 .owner = THIS_MODULE, 262 .act = tcf_pedit, 263 .dump = tcf_pedit_dump, 264 .cleanup = tcf_pedit_cleanup, 265 .lookup = tcf_hash_search, 266 .init = tcf_pedit_init, 267 .walk = tcf_generic_walker 268 }; 269 270 MODULE_AUTHOR("Jamal Hadi Salim(2002-4)"); 271 MODULE_DESCRIPTION("Generic Packet Editor actions"); 272 MODULE_LICENSE("GPL"); 273 274 static int __init 275 pedit_init_module(void) 276 { 277 return tcf_register_action(&act_pedit_ops); 278 } 279 280 static void __exit 281 pedit_cleanup_module(void) 282 { 283 tcf_unregister_action(&act_pedit_ops); 284 } 285 286 module_init(pedit_init_module); 287 module_exit(pedit_cleanup_module); 288 289