br_netfilter.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978
  1. /*
  2. * Handle firewalling
  3. * Linux ethernet bridge
  4. *
  5. * Authors:
  6. * Lennert Buytenhek <buytenh@gnu.org>
  7. * Bart De Schuymer (maintainer) <bdschuym@pandora.be>
  8. *
  9. * Changes:
  10. * Apr 29 2003: physdev module support (bdschuym)
  11. * Jun 19 2003: let arptables see bridged ARP traffic (bdschuym)
  12. * Oct 06 2003: filter encapsulated IP/ARP VLAN traffic on untagged bridge
  13. * (bdschuym)
  14. * Sep 01 2004: add IPv6 filtering (bdschuym)
  15. *
  16. * This program is free software; you can redistribute it and/or
  17. * modify it under the terms of the GNU General Public License
  18. * as published by the Free Software Foundation; either version
  19. * 2 of the License, or (at your option) any later version.
  20. *
  21. * Lennert dedicates this file to Kerstin Wurdinger.
  22. */
  23. #include <linux/module.h>
  24. #include <linux/kernel.h>
  25. #include <linux/ip.h>
  26. #include <linux/netdevice.h>
  27. #include <linux/skbuff.h>
  28. #include <linux/if_arp.h>
  29. #include <linux/if_ether.h>
  30. #include <linux/if_vlan.h>
  31. #include <linux/netfilter_bridge.h>
  32. #include <linux/netfilter_ipv4.h>
  33. #include <linux/netfilter_ipv6.h>
  34. #include <linux/netfilter_arp.h>
  35. #include <linux/in_route.h>
  36. #include <linux/inetdevice.h>
  37. #include <net/ip.h>
  38. #include <net/ipv6.h>
  39. #include <net/route.h>
  40. #include <asm/uaccess.h>
  41. #include "br_private.h"
  42. #ifdef CONFIG_SYSCTL
  43. #include <linux/sysctl.h>
  44. #endif
  45. #define skb_origaddr(skb) (((struct bridge_skb_cb *) \
  46. (skb->nf_bridge->data))->daddr.ipv4)
  47. #define store_orig_dstaddr(skb) (skb_origaddr(skb) = (skb)->nh.iph->daddr)
  48. #define dnat_took_place(skb) (skb_origaddr(skb) != (skb)->nh.iph->daddr)
  49. #ifdef CONFIG_SYSCTL
  50. static struct ctl_table_header *brnf_sysctl_header;
  51. static int brnf_call_iptables __read_mostly = 1;
  52. static int brnf_call_ip6tables __read_mostly = 1;
  53. static int brnf_call_arptables __read_mostly = 1;
  54. static int brnf_filter_vlan_tagged __read_mostly = 1;
  55. #else
  56. #define brnf_filter_vlan_tagged 1
  57. #endif
  58. static inline __be16 vlan_proto(const struct sk_buff *skb)
  59. {
  60. return vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
  61. }
  62. #define IS_VLAN_IP(skb) \
  63. (skb->protocol == htons(ETH_P_8021Q) && \
  64. vlan_proto(skb) == htons(ETH_P_IP) && \
  65. brnf_filter_vlan_tagged)
  66. #define IS_VLAN_IPV6(skb) \
  67. (skb->protocol == htons(ETH_P_8021Q) && \
  68. vlan_proto(skb) == htons(ETH_P_IPV6) &&\
  69. brnf_filter_vlan_tagged)
  70. #define IS_VLAN_ARP(skb) \
  71. (skb->protocol == htons(ETH_P_8021Q) && \
  72. vlan_proto(skb) == htons(ETH_P_ARP) && \
  73. brnf_filter_vlan_tagged)
  74. /* We need these fake structures to make netfilter happy --
  75. * lots of places assume that skb->dst != NULL, which isn't
  76. * all that unreasonable.
  77. *
  78. * Currently, we fill in the PMTU entry because netfilter
  79. * refragmentation needs it, and the rt_flags entry because
  80. * ipt_REJECT needs it. Future netfilter modules might
  81. * require us to fill additional fields. */
  82. static struct net_device __fake_net_device = {
  83. .hard_header_len = ETH_HLEN
  84. };
  85. static struct rtable __fake_rtable = {
  86. .u = {
  87. .dst = {
  88. .__refcnt = ATOMIC_INIT(1),
  89. .dev = &__fake_net_device,
  90. .path = &__fake_rtable.u.dst,
  91. .metrics = {[RTAX_MTU - 1] = 1500},
  92. .flags = DST_NOXFRM,
  93. }
  94. },
  95. .rt_flags = 0,
  96. };
  97. static inline struct net_device *bridge_parent(const struct net_device *dev)
  98. {
  99. struct net_bridge_port *port = rcu_dereference(dev->br_port);
  100. return port ? port->br->dev : NULL;
  101. }
  102. static inline struct nf_bridge_info *nf_bridge_alloc(struct sk_buff *skb)
  103. {
  104. skb->nf_bridge = kzalloc(sizeof(struct nf_bridge_info), GFP_ATOMIC);
  105. if (likely(skb->nf_bridge))
  106. atomic_set(&(skb->nf_bridge->use), 1);
  107. return skb->nf_bridge;
  108. }
  109. static inline void nf_bridge_save_header(struct sk_buff *skb)
  110. {
  111. int header_size = ETH_HLEN;
  112. if (skb->protocol == htons(ETH_P_8021Q))
  113. header_size += VLAN_HLEN;
  114. memcpy(skb->nf_bridge->data, skb->data - header_size, header_size);
  115. }
  116. /*
  117. * When forwarding bridge frames, we save a copy of the original
  118. * header before processing.
  119. */
  120. int nf_bridge_copy_header(struct sk_buff *skb)
  121. {
  122. int err;
  123. int header_size = ETH_HLEN;
  124. if (skb->protocol == htons(ETH_P_8021Q))
  125. header_size += VLAN_HLEN;
  126. err = skb_cow(skb, header_size);
  127. if (err)
  128. return err;
  129. memcpy(skb->data - header_size, skb->nf_bridge->data, header_size);
  130. if (skb->protocol == htons(ETH_P_8021Q))
  131. __skb_push(skb, VLAN_HLEN);
  132. return 0;
  133. }
  134. /* PF_BRIDGE/PRE_ROUTING *********************************************/
  135. /* Undo the changes made for ip6tables PREROUTING and continue the
  136. * bridge PRE_ROUTING hook. */
  137. static int br_nf_pre_routing_finish_ipv6(struct sk_buff *skb)
  138. {
  139. struct nf_bridge_info *nf_bridge = skb->nf_bridge;
  140. if (nf_bridge->mask & BRNF_PKT_TYPE) {
  141. skb->pkt_type = PACKET_OTHERHOST;
  142. nf_bridge->mask ^= BRNF_PKT_TYPE;
  143. }
  144. nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING;
  145. skb->dst = (struct dst_entry *)&__fake_rtable;
  146. dst_hold(skb->dst);
  147. skb->dev = nf_bridge->physindev;
  148. if (skb->protocol == htons(ETH_P_8021Q)) {
  149. skb_push(skb, VLAN_HLEN);
  150. skb->nh.raw -= VLAN_HLEN;
  151. }
  152. NF_HOOK_THRESH(PF_BRIDGE, NF_BR_PRE_ROUTING, skb, skb->dev, NULL,
  153. br_handle_frame_finish, 1);
  154. return 0;
  155. }
  156. static void __br_dnat_complain(void)
  157. {
  158. static unsigned long last_complaint;
  159. if (jiffies - last_complaint >= 5 * HZ) {
  160. printk(KERN_WARNING "Performing cross-bridge DNAT requires IP "
  161. "forwarding to be enabled\n");
  162. last_complaint = jiffies;
  163. }
  164. }
  165. /* This requires some explaining. If DNAT has taken place,
  166. * we will need to fix up the destination Ethernet address,
  167. * and this is a tricky process.
  168. *
  169. * There are two cases to consider:
  170. * 1. The packet was DNAT'ed to a device in the same bridge
  171. * port group as it was received on. We can still bridge
  172. * the packet.
  173. * 2. The packet was DNAT'ed to a different device, either
  174. * a non-bridged device or another bridge port group.
  175. * The packet will need to be routed.
  176. *
  177. * The correct way of distinguishing between these two cases is to
  178. * call ip_route_input() and to look at skb->dst->dev, which is
  179. * changed to the destination device if ip_route_input() succeeds.
  180. *
  181. * Let us first consider the case that ip_route_input() succeeds:
  182. *
  183. * If skb->dst->dev equals the logical bridge device the packet
  184. * came in on, we can consider this bridging. We then call
  185. * skb->dst->output() which will make the packet enter br_nf_local_out()
  186. * not much later. In that function it is assured that the iptables
  187. * FORWARD chain is traversed for the packet.
  188. *
  189. * Otherwise, the packet is considered to be routed and we just
  190. * change the destination MAC address so that the packet will
  191. * later be passed up to the IP stack to be routed. For a redirected
  192. * packet, ip_route_input() will give back the localhost as output device,
  193. * which differs from the bridge device.
  194. *
  195. * Let us now consider the case that ip_route_input() fails:
  196. *
  197. * This can be because the destination address is martian, in which case
  198. * the packet will be dropped.
  199. * After a "echo '0' > /proc/sys/net/ipv4/ip_forward" ip_route_input()
  200. * will fail, while __ip_route_output_key() will return success. The source
  201. * address for __ip_route_output_key() is set to zero, so __ip_route_output_key
  202. * thinks we're handling a locally generated packet and won't care
  203. * if IP forwarding is allowed. We send a warning message to the users's
  204. * log telling her to put IP forwarding on.
  205. *
  206. * ip_route_input() will also fail if there is no route available.
  207. * In that case we just drop the packet.
  208. *
  209. * --Lennert, 20020411
  210. * --Bart, 20020416 (updated)
  211. * --Bart, 20021007 (updated)
  212. * --Bart, 20062711 (updated) */
  213. static int br_nf_pre_routing_finish_bridge(struct sk_buff *skb)
  214. {
  215. if (skb->pkt_type == PACKET_OTHERHOST) {
  216. skb->pkt_type = PACKET_HOST;
  217. skb->nf_bridge->mask |= BRNF_PKT_TYPE;
  218. }
  219. skb->nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING;
  220. skb->dev = bridge_parent(skb->dev);
  221. if (!skb->dev)
  222. kfree_skb(skb);
  223. else {
  224. if (skb->protocol == htons(ETH_P_8021Q)) {
  225. skb_pull(skb, VLAN_HLEN);
  226. skb->nh.raw += VLAN_HLEN;
  227. }
  228. skb->dst->output(skb);
  229. }
  230. return 0;
  231. }
  232. static int br_nf_pre_routing_finish(struct sk_buff *skb)
  233. {
  234. struct net_device *dev = skb->dev;
  235. struct iphdr *iph = skb->nh.iph;
  236. struct nf_bridge_info *nf_bridge = skb->nf_bridge;
  237. int err;
  238. if (nf_bridge->mask & BRNF_PKT_TYPE) {
  239. skb->pkt_type = PACKET_OTHERHOST;
  240. nf_bridge->mask ^= BRNF_PKT_TYPE;
  241. }
  242. nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING;
  243. if (dnat_took_place(skb)) {
  244. if ((err = ip_route_input(skb, iph->daddr, iph->saddr, iph->tos, dev))) {
  245. struct rtable *rt;
  246. struct flowi fl = {
  247. .nl_u = {
  248. .ip4_u = {
  249. .daddr = iph->daddr,
  250. .saddr = 0,
  251. .tos = RT_TOS(iph->tos) },
  252. },
  253. .proto = 0,
  254. };
  255. struct in_device *in_dev = in_dev_get(dev);
  256. /* If err equals -EHOSTUNREACH the error is due to a
  257. * martian destination or due to the fact that
  258. * forwarding is disabled. For most martian packets,
  259. * ip_route_output_key() will fail. It won't fail for 2 types of
  260. * martian destinations: loopback destinations and destination
  261. * 0.0.0.0. In both cases the packet will be dropped because the
  262. * destination is the loopback device and not the bridge. */
  263. if (err != -EHOSTUNREACH || !in_dev || IN_DEV_FORWARD(in_dev))
  264. goto free_skb;
  265. if (!ip_route_output_key(&rt, &fl)) {
  266. /* - Bridged-and-DNAT'ed traffic doesn't
  267. * require ip_forwarding. */
  268. if (((struct dst_entry *)rt)->dev == dev) {
  269. skb->dst = (struct dst_entry *)rt;
  270. goto bridged_dnat;
  271. }
  272. /* we are sure that forwarding is disabled, so printing
  273. * this message is no problem. Note that the packet could
  274. * still have a martian destination address, in which case
  275. * the packet could be dropped even if forwarding were enabled */
  276. __br_dnat_complain();
  277. dst_release((struct dst_entry *)rt);
  278. }
  279. free_skb:
  280. kfree_skb(skb);
  281. return 0;
  282. } else {
  283. if (skb->dst->dev == dev) {
  284. bridged_dnat:
  285. /* Tell br_nf_local_out this is a
  286. * bridged frame */
  287. nf_bridge->mask |= BRNF_BRIDGED_DNAT;
  288. skb->dev = nf_bridge->physindev;
  289. if (skb->protocol ==
  290. htons(ETH_P_8021Q)) {
  291. skb_push(skb, VLAN_HLEN);
  292. skb->nh.raw -= VLAN_HLEN;
  293. }
  294. NF_HOOK_THRESH(PF_BRIDGE, NF_BR_PRE_ROUTING,
  295. skb, skb->dev, NULL,
  296. br_nf_pre_routing_finish_bridge,
  297. 1);
  298. return 0;
  299. }
  300. memcpy(eth_hdr(skb)->h_dest, dev->dev_addr, ETH_ALEN);
  301. skb->pkt_type = PACKET_HOST;
  302. }
  303. } else {
  304. skb->dst = (struct dst_entry *)&__fake_rtable;
  305. dst_hold(skb->dst);
  306. }
  307. skb->dev = nf_bridge->physindev;
  308. if (skb->protocol == htons(ETH_P_8021Q)) {
  309. skb_push(skb, VLAN_HLEN);
  310. skb->nh.raw -= VLAN_HLEN;
  311. }
  312. NF_HOOK_THRESH(PF_BRIDGE, NF_BR_PRE_ROUTING, skb, skb->dev, NULL,
  313. br_handle_frame_finish, 1);
  314. return 0;
  315. }
  316. /* Some common code for IPv4/IPv6 */
  317. static struct net_device *setup_pre_routing(struct sk_buff *skb)
  318. {
  319. struct nf_bridge_info *nf_bridge = skb->nf_bridge;
  320. if (skb->pkt_type == PACKET_OTHERHOST) {
  321. skb->pkt_type = PACKET_HOST;
  322. nf_bridge->mask |= BRNF_PKT_TYPE;
  323. }
  324. nf_bridge->mask |= BRNF_NF_BRIDGE_PREROUTING;
  325. nf_bridge->physindev = skb->dev;
  326. skb->dev = bridge_parent(skb->dev);
  327. return skb->dev;
  328. }
  329. /* We only check the length. A bridge shouldn't do any hop-by-hop stuff anyway */
  330. static int check_hbh_len(struct sk_buff *skb)
  331. {
  332. unsigned char *raw = (u8 *) (skb->nh.ipv6h + 1);
  333. u32 pkt_len;
  334. int off = raw - skb->nh.raw;
  335. int len = (raw[1] + 1) << 3;
  336. if ((raw + len) - skb->data > skb_headlen(skb))
  337. goto bad;
  338. off += 2;
  339. len -= 2;
  340. while (len > 0) {
  341. int optlen = skb->nh.raw[off + 1] + 2;
  342. switch (skb->nh.raw[off]) {
  343. case IPV6_TLV_PAD0:
  344. optlen = 1;
  345. break;
  346. case IPV6_TLV_PADN:
  347. break;
  348. case IPV6_TLV_JUMBO:
  349. if (skb->nh.raw[off + 1] != 4 || (off & 3) != 2)
  350. goto bad;
  351. pkt_len = ntohl(*(__be32 *) (skb->nh.raw + off + 2));
  352. if (pkt_len <= IPV6_MAXPLEN ||
  353. skb->nh.ipv6h->payload_len)
  354. goto bad;
  355. if (pkt_len > skb->len - sizeof(struct ipv6hdr))
  356. goto bad;
  357. if (pskb_trim_rcsum(skb,
  358. pkt_len + sizeof(struct ipv6hdr)))
  359. goto bad;
  360. break;
  361. default:
  362. if (optlen > len)
  363. goto bad;
  364. break;
  365. }
  366. off += optlen;
  367. len -= optlen;
  368. }
  369. if (len == 0)
  370. return 0;
  371. bad:
  372. return -1;
  373. }
  374. /* Replicate the checks that IPv6 does on packet reception and pass the packet
  375. * to ip6tables, which doesn't support NAT, so things are fairly simple. */
  376. static unsigned int br_nf_pre_routing_ipv6(unsigned int hook,
  377. struct sk_buff *skb,
  378. const struct net_device *in,
  379. const struct net_device *out,
  380. int (*okfn)(struct sk_buff *))
  381. {
  382. struct ipv6hdr *hdr;
  383. u32 pkt_len;
  384. if (skb->len < sizeof(struct ipv6hdr))
  385. goto inhdr_error;
  386. if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
  387. goto inhdr_error;
  388. hdr = skb->nh.ipv6h;
  389. if (hdr->version != 6)
  390. goto inhdr_error;
  391. pkt_len = ntohs(hdr->payload_len);
  392. if (pkt_len || hdr->nexthdr != NEXTHDR_HOP) {
  393. if (pkt_len + sizeof(struct ipv6hdr) > skb->len)
  394. goto inhdr_error;
  395. if (pskb_trim_rcsum(skb, pkt_len + sizeof(struct ipv6hdr)))
  396. goto inhdr_error;
  397. }
  398. if (hdr->nexthdr == NEXTHDR_HOP && check_hbh_len(skb))
  399. goto inhdr_error;
  400. nf_bridge_put(skb->nf_bridge);
  401. if (!nf_bridge_alloc(skb))
  402. return NF_DROP;
  403. if (!setup_pre_routing(skb))
  404. return NF_DROP;
  405. NF_HOOK(PF_INET6, NF_IP6_PRE_ROUTING, skb, skb->dev, NULL,
  406. br_nf_pre_routing_finish_ipv6);
  407. return NF_STOLEN;
  408. inhdr_error:
  409. return NF_DROP;
  410. }
  411. /* Direct IPv6 traffic to br_nf_pre_routing_ipv6.
  412. * Replicate the checks that IPv4 does on packet reception.
  413. * Set skb->dev to the bridge device (i.e. parent of the
  414. * receiving device) to make netfilter happy, the REDIRECT
  415. * target in particular. Save the original destination IP
  416. * address to be able to detect DNAT afterwards. */
  417. static unsigned int br_nf_pre_routing(unsigned int hook, struct sk_buff **pskb,
  418. const struct net_device *in,
  419. const struct net_device *out,
  420. int (*okfn)(struct sk_buff *))
  421. {
  422. struct iphdr *iph;
  423. __u32 len;
  424. struct sk_buff *skb = *pskb;
  425. if (skb->protocol == htons(ETH_P_IPV6) || IS_VLAN_IPV6(skb)) {
  426. #ifdef CONFIG_SYSCTL
  427. if (!brnf_call_ip6tables)
  428. return NF_ACCEPT;
  429. #endif
  430. if ((skb = skb_share_check(*pskb, GFP_ATOMIC)) == NULL)
  431. goto out;
  432. if (skb->protocol == htons(ETH_P_8021Q)) {
  433. skb_pull_rcsum(skb, VLAN_HLEN);
  434. skb->nh.raw += VLAN_HLEN;
  435. }
  436. return br_nf_pre_routing_ipv6(hook, skb, in, out, okfn);
  437. }
  438. #ifdef CONFIG_SYSCTL
  439. if (!brnf_call_iptables)
  440. return NF_ACCEPT;
  441. #endif
  442. if (skb->protocol != htons(ETH_P_IP) && !IS_VLAN_IP(skb))
  443. return NF_ACCEPT;
  444. if ((skb = skb_share_check(*pskb, GFP_ATOMIC)) == NULL)
  445. goto out;
  446. if (skb->protocol == htons(ETH_P_8021Q)) {
  447. skb_pull_rcsum(skb, VLAN_HLEN);
  448. skb->nh.raw += VLAN_HLEN;
  449. }
  450. if (!pskb_may_pull(skb, sizeof(struct iphdr)))
  451. goto inhdr_error;
  452. iph = skb->nh.iph;
  453. if (iph->ihl < 5 || iph->version != 4)
  454. goto inhdr_error;
  455. if (!pskb_may_pull(skb, 4 * iph->ihl))
  456. goto inhdr_error;
  457. iph = skb->nh.iph;
  458. if (ip_fast_csum((__u8 *) iph, iph->ihl) != 0)
  459. goto inhdr_error;
  460. len = ntohs(iph->tot_len);
  461. if (skb->len < len || len < 4 * iph->ihl)
  462. goto inhdr_error;
  463. pskb_trim_rcsum(skb, len);
  464. nf_bridge_put(skb->nf_bridge);
  465. if (!nf_bridge_alloc(skb))
  466. return NF_DROP;
  467. if (!setup_pre_routing(skb))
  468. return NF_DROP;
  469. store_orig_dstaddr(skb);
  470. NF_HOOK(PF_INET, NF_IP_PRE_ROUTING, skb, skb->dev, NULL,
  471. br_nf_pre_routing_finish);
  472. return NF_STOLEN;
  473. inhdr_error:
  474. // IP_INC_STATS_BH(IpInHdrErrors);
  475. out:
  476. return NF_DROP;
  477. }
  478. /* PF_BRIDGE/LOCAL_IN ************************************************/
  479. /* The packet is locally destined, which requires a real
  480. * dst_entry, so detach the fake one. On the way up, the
  481. * packet would pass through PRE_ROUTING again (which already
  482. * took place when the packet entered the bridge), but we
  483. * register an IPv4 PRE_ROUTING 'sabotage' hook that will
  484. * prevent this from happening. */
  485. static unsigned int br_nf_local_in(unsigned int hook, struct sk_buff **pskb,
  486. const struct net_device *in,
  487. const struct net_device *out,
  488. int (*okfn)(struct sk_buff *))
  489. {
  490. struct sk_buff *skb = *pskb;
  491. if (skb->dst == (struct dst_entry *)&__fake_rtable) {
  492. dst_release(skb->dst);
  493. skb->dst = NULL;
  494. }
  495. return NF_ACCEPT;
  496. }
  497. /* PF_BRIDGE/FORWARD *************************************************/
  498. static int br_nf_forward_finish(struct sk_buff *skb)
  499. {
  500. struct nf_bridge_info *nf_bridge = skb->nf_bridge;
  501. struct net_device *in;
  502. if (skb->protocol != htons(ETH_P_ARP) && !IS_VLAN_ARP(skb)) {
  503. in = nf_bridge->physindev;
  504. if (nf_bridge->mask & BRNF_PKT_TYPE) {
  505. skb->pkt_type = PACKET_OTHERHOST;
  506. nf_bridge->mask ^= BRNF_PKT_TYPE;
  507. }
  508. } else {
  509. in = *((struct net_device **)(skb->cb));
  510. }
  511. if (skb->protocol == htons(ETH_P_8021Q)) {
  512. skb_push(skb, VLAN_HLEN);
  513. skb->nh.raw -= VLAN_HLEN;
  514. }
  515. NF_HOOK_THRESH(PF_BRIDGE, NF_BR_FORWARD, skb, in,
  516. skb->dev, br_forward_finish, 1);
  517. return 0;
  518. }
  519. /* This is the 'purely bridged' case. For IP, we pass the packet to
  520. * netfilter with indev and outdev set to the bridge device,
  521. * but we are still able to filter on the 'real' indev/outdev
  522. * because of the physdev module. For ARP, indev and outdev are the
  523. * bridge ports. */
  524. static unsigned int br_nf_forward_ip(unsigned int hook, struct sk_buff **pskb,
  525. const struct net_device *in,
  526. const struct net_device *out,
  527. int (*okfn)(struct sk_buff *))
  528. {
  529. struct sk_buff *skb = *pskb;
  530. struct nf_bridge_info *nf_bridge;
  531. struct net_device *parent;
  532. int pf;
  533. if (!skb->nf_bridge)
  534. return NF_ACCEPT;
  535. parent = bridge_parent(out);
  536. if (!parent)
  537. return NF_DROP;
  538. if (skb->protocol == htons(ETH_P_IP) || IS_VLAN_IP(skb))
  539. pf = PF_INET;
  540. else
  541. pf = PF_INET6;
  542. if (skb->protocol == htons(ETH_P_8021Q)) {
  543. skb_pull(*pskb, VLAN_HLEN);
  544. (*pskb)->nh.raw += VLAN_HLEN;
  545. }
  546. nf_bridge = skb->nf_bridge;
  547. if (skb->pkt_type == PACKET_OTHERHOST) {
  548. skb->pkt_type = PACKET_HOST;
  549. nf_bridge->mask |= BRNF_PKT_TYPE;
  550. }
  551. /* The physdev module checks on this */
  552. nf_bridge->mask |= BRNF_BRIDGED;
  553. nf_bridge->physoutdev = skb->dev;
  554. NF_HOOK(pf, NF_IP_FORWARD, skb, bridge_parent(in), parent,
  555. br_nf_forward_finish);
  556. return NF_STOLEN;
  557. }
  558. static unsigned int br_nf_forward_arp(unsigned int hook, struct sk_buff **pskb,
  559. const struct net_device *in,
  560. const struct net_device *out,
  561. int (*okfn)(struct sk_buff *))
  562. {
  563. struct sk_buff *skb = *pskb;
  564. struct net_device **d = (struct net_device **)(skb->cb);
  565. #ifdef CONFIG_SYSCTL
  566. if (!brnf_call_arptables)
  567. return NF_ACCEPT;
  568. #endif
  569. if (skb->protocol != htons(ETH_P_ARP)) {
  570. if (!IS_VLAN_ARP(skb))
  571. return NF_ACCEPT;
  572. skb_pull(*pskb, VLAN_HLEN);
  573. (*pskb)->nh.raw += VLAN_HLEN;
  574. }
  575. if (skb->nh.arph->ar_pln != 4) {
  576. if (IS_VLAN_ARP(skb)) {
  577. skb_push(*pskb, VLAN_HLEN);
  578. (*pskb)->nh.raw -= VLAN_HLEN;
  579. }
  580. return NF_ACCEPT;
  581. }
  582. *d = (struct net_device *)in;
  583. NF_HOOK(NF_ARP, NF_ARP_FORWARD, skb, (struct net_device *)in,
  584. (struct net_device *)out, br_nf_forward_finish);
  585. return NF_STOLEN;
  586. }
  587. /* PF_BRIDGE/LOCAL_OUT ***********************************************
  588. *
  589. * This function sees both locally originated IP packets and forwarded
  590. * IP packets (in both cases the destination device is a bridge
  591. * device). It also sees bridged-and-DNAT'ed packets.
  592. *
  593. * If (nf_bridge->mask & BRNF_BRIDGED_DNAT) then the packet is bridged
  594. * and we fake the PF_BRIDGE/FORWARD hook. The function br_nf_forward()
  595. * will then fake the PF_INET/FORWARD hook. br_nf_local_out() has priority
  596. * NF_BR_PRI_FIRST, so no relevant PF_BRIDGE/INPUT functions have been nor
  597. * will be executed.
  598. */
  599. static unsigned int br_nf_local_out(unsigned int hook, struct sk_buff **pskb,
  600. const struct net_device *in,
  601. const struct net_device *out,
  602. int (*okfn)(struct sk_buff *))
  603. {
  604. struct net_device *realindev;
  605. struct sk_buff *skb = *pskb;
  606. struct nf_bridge_info *nf_bridge;
  607. if (!skb->nf_bridge)
  608. return NF_ACCEPT;
  609. nf_bridge = skb->nf_bridge;
  610. if (!(nf_bridge->mask & BRNF_BRIDGED_DNAT))
  611. return NF_ACCEPT;
  612. /* Bridged, take PF_BRIDGE/FORWARD.
  613. * (see big note in front of br_nf_pre_routing_finish) */
  614. nf_bridge->physoutdev = skb->dev;
  615. realindev = nf_bridge->physindev;
  616. if (nf_bridge->mask & BRNF_PKT_TYPE) {
  617. skb->pkt_type = PACKET_OTHERHOST;
  618. nf_bridge->mask ^= BRNF_PKT_TYPE;
  619. }
  620. if (skb->protocol == htons(ETH_P_8021Q)) {
  621. skb_push(skb, VLAN_HLEN);
  622. skb->nh.raw -= VLAN_HLEN;
  623. }
  624. NF_HOOK(PF_BRIDGE, NF_BR_FORWARD, skb, realindev, skb->dev,
  625. br_forward_finish);
  626. return NF_STOLEN;
  627. }
  628. static int br_nf_dev_queue_xmit(struct sk_buff *skb)
  629. {
  630. if (skb->protocol == htons(ETH_P_IP) &&
  631. skb->len > skb->dev->mtu &&
  632. !skb_is_gso(skb))
  633. return ip_fragment(skb, br_dev_queue_push_xmit);
  634. else
  635. return br_dev_queue_push_xmit(skb);
  636. }
  637. /* PF_BRIDGE/POST_ROUTING ********************************************/
  638. static unsigned int br_nf_post_routing(unsigned int hook, struct sk_buff **pskb,
  639. const struct net_device *in,
  640. const struct net_device *out,
  641. int (*okfn)(struct sk_buff *))
  642. {
  643. struct sk_buff *skb = *pskb;
  644. struct nf_bridge_info *nf_bridge = (*pskb)->nf_bridge;
  645. struct net_device *realoutdev = bridge_parent(skb->dev);
  646. int pf;
  647. #ifdef CONFIG_NETFILTER_DEBUG
  648. /* Be very paranoid. This probably won't happen anymore, but let's
  649. * keep the check just to be sure... */
  650. if (skb->mac.raw < skb->head || skb->mac.raw + ETH_HLEN > skb->data) {
  651. printk(KERN_CRIT "br_netfilter: Argh!! br_nf_post_routing: "
  652. "bad mac.raw pointer.\n");
  653. goto print_error;
  654. }
  655. #endif
  656. if (!nf_bridge)
  657. return NF_ACCEPT;
  658. if (!realoutdev)
  659. return NF_DROP;
  660. if (skb->protocol == htons(ETH_P_IP) || IS_VLAN_IP(skb))
  661. pf = PF_INET;
  662. else
  663. pf = PF_INET6;
  664. #ifdef CONFIG_NETFILTER_DEBUG
  665. if (skb->dst == NULL) {
  666. printk(KERN_INFO "br_netfilter post_routing: skb->dst == NULL\n");
  667. goto print_error;
  668. }
  669. #endif
  670. /* We assume any code from br_dev_queue_push_xmit onwards doesn't care
  671. * about the value of skb->pkt_type. */
  672. if (skb->pkt_type == PACKET_OTHERHOST) {
  673. skb->pkt_type = PACKET_HOST;
  674. nf_bridge->mask |= BRNF_PKT_TYPE;
  675. }
  676. if (skb->protocol == htons(ETH_P_8021Q)) {
  677. skb_pull(skb, VLAN_HLEN);
  678. skb->nh.raw += VLAN_HLEN;
  679. }
  680. nf_bridge_save_header(skb);
  681. #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
  682. if (nf_bridge->netoutdev)
  683. realoutdev = nf_bridge->netoutdev;
  684. #endif
  685. NF_HOOK(pf, NF_IP_POST_ROUTING, skb, NULL, realoutdev,
  686. br_nf_dev_queue_xmit);
  687. return NF_STOLEN;
  688. #ifdef CONFIG_NETFILTER_DEBUG
  689. print_error:
  690. if (skb->dev != NULL) {
  691. printk("[%s]", skb->dev->name);
  692. if (realoutdev)
  693. printk("[%s]", realoutdev->name);
  694. }
  695. printk(" head:%p, raw:%p, data:%p\n", skb->head, skb->mac.raw,
  696. skb->data);
  697. dump_stack();
  698. return NF_ACCEPT;
  699. #endif
  700. }
  701. /* IP/SABOTAGE *****************************************************/
  702. /* Don't hand locally destined packets to PF_INET(6)/PRE_ROUTING
  703. * for the second time. */
  704. static unsigned int ip_sabotage_in(unsigned int hook, struct sk_buff **pskb,
  705. const struct net_device *in,
  706. const struct net_device *out,
  707. int (*okfn)(struct sk_buff *))
  708. {
  709. if ((*pskb)->nf_bridge &&
  710. !((*pskb)->nf_bridge->mask & BRNF_NF_BRIDGE_PREROUTING)) {
  711. return NF_STOP;
  712. }
  713. return NF_ACCEPT;
  714. }
  715. /* For br_nf_local_out we need (prio = NF_BR_PRI_FIRST), to insure that innocent
  716. * PF_BRIDGE/NF_BR_LOCAL_OUT functions don't get bridged traffic as input.
  717. * For br_nf_post_routing, we need (prio = NF_BR_PRI_LAST), because
  718. * ip_refrag() can return NF_STOLEN. */
  719. static struct nf_hook_ops br_nf_ops[] = {
  720. { .hook = br_nf_pre_routing,
  721. .owner = THIS_MODULE,
  722. .pf = PF_BRIDGE,
  723. .hooknum = NF_BR_PRE_ROUTING,
  724. .priority = NF_BR_PRI_BRNF, },
  725. { .hook = br_nf_local_in,
  726. .owner = THIS_MODULE,
  727. .pf = PF_BRIDGE,
  728. .hooknum = NF_BR_LOCAL_IN,
  729. .priority = NF_BR_PRI_BRNF, },
  730. { .hook = br_nf_forward_ip,
  731. .owner = THIS_MODULE,
  732. .pf = PF_BRIDGE,
  733. .hooknum = NF_BR_FORWARD,
  734. .priority = NF_BR_PRI_BRNF - 1, },
  735. { .hook = br_nf_forward_arp,
  736. .owner = THIS_MODULE,
  737. .pf = PF_BRIDGE,
  738. .hooknum = NF_BR_FORWARD,
  739. .priority = NF_BR_PRI_BRNF, },
  740. { .hook = br_nf_local_out,
  741. .owner = THIS_MODULE,
  742. .pf = PF_BRIDGE,
  743. .hooknum = NF_BR_LOCAL_OUT,
  744. .priority = NF_BR_PRI_FIRST, },
  745. { .hook = br_nf_post_routing,
  746. .owner = THIS_MODULE,
  747. .pf = PF_BRIDGE,
  748. .hooknum = NF_BR_POST_ROUTING,
  749. .priority = NF_BR_PRI_LAST, },
  750. { .hook = ip_sabotage_in,
  751. .owner = THIS_MODULE,
  752. .pf = PF_INET,
  753. .hooknum = NF_IP_PRE_ROUTING,
  754. .priority = NF_IP_PRI_FIRST, },
  755. { .hook = ip_sabotage_in,
  756. .owner = THIS_MODULE,
  757. .pf = PF_INET6,
  758. .hooknum = NF_IP6_PRE_ROUTING,
  759. .priority = NF_IP6_PRI_FIRST, },
  760. };
  761. #ifdef CONFIG_SYSCTL
  762. static
  763. int brnf_sysctl_call_tables(ctl_table * ctl, int write, struct file *filp,
  764. void __user * buffer, size_t * lenp, loff_t * ppos)
  765. {
  766. int ret;
  767. ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
  768. if (write && *(int *)(ctl->data))
  769. *(int *)(ctl->data) = 1;
  770. return ret;
  771. }
  772. static ctl_table brnf_table[] = {
  773. {
  774. .ctl_name = NET_BRIDGE_NF_CALL_ARPTABLES,
  775. .procname = "bridge-nf-call-arptables",
  776. .data = &brnf_call_arptables,
  777. .maxlen = sizeof(int),
  778. .mode = 0644,
  779. .proc_handler = &brnf_sysctl_call_tables,
  780. },
  781. {
  782. .ctl_name = NET_BRIDGE_NF_CALL_IPTABLES,
  783. .procname = "bridge-nf-call-iptables",
  784. .data = &brnf_call_iptables,
  785. .maxlen = sizeof(int),
  786. .mode = 0644,
  787. .proc_handler = &brnf_sysctl_call_tables,
  788. },
  789. {
  790. .ctl_name = NET_BRIDGE_NF_CALL_IP6TABLES,
  791. .procname = "bridge-nf-call-ip6tables",
  792. .data = &brnf_call_ip6tables,
  793. .maxlen = sizeof(int),
  794. .mode = 0644,
  795. .proc_handler = &brnf_sysctl_call_tables,
  796. },
  797. {
  798. .ctl_name = NET_BRIDGE_NF_FILTER_VLAN_TAGGED,
  799. .procname = "bridge-nf-filter-vlan-tagged",
  800. .data = &brnf_filter_vlan_tagged,
  801. .maxlen = sizeof(int),
  802. .mode = 0644,
  803. .proc_handler = &brnf_sysctl_call_tables,
  804. },
  805. { .ctl_name = 0 }
  806. };
  807. static ctl_table brnf_bridge_table[] = {
  808. {
  809. .ctl_name = NET_BRIDGE,
  810. .procname = "bridge",
  811. .mode = 0555,
  812. .child = brnf_table,
  813. },
  814. { .ctl_name = 0 }
  815. };
  816. static ctl_table brnf_net_table[] = {
  817. {
  818. .ctl_name = CTL_NET,
  819. .procname = "net",
  820. .mode = 0555,
  821. .child = brnf_bridge_table,
  822. },
  823. { .ctl_name = 0 }
  824. };
  825. #endif
  826. int __init br_netfilter_init(void)
  827. {
  828. int ret;
  829. ret = nf_register_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
  830. if (ret < 0)
  831. return ret;
  832. #ifdef CONFIG_SYSCTL
  833. brnf_sysctl_header = register_sysctl_table(brnf_net_table);
  834. if (brnf_sysctl_header == NULL) {
  835. printk(KERN_WARNING
  836. "br_netfilter: can't register to sysctl.\n");
  837. nf_unregister_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
  838. return -ENOMEM;
  839. }
  840. #endif
  841. printk(KERN_NOTICE "Bridge firewalling registered\n");
  842. return 0;
  843. }
  844. void br_netfilter_fini(void)
  845. {
  846. nf_unregister_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
  847. #ifdef CONFIG_SYSCTL
  848. unregister_sysctl_table(brnf_sysctl_header);
  849. #endif
  850. }