1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2007 - 2011 Realtek Corporation. i*/
3 
4 #define _RTW_BR_EXT_C_
5 
6 #include "../include/linux/if_arp.h"
7 #include "../include/net/ip.h"
8 #include "../include/linux/atalk.h"
9 #include "../include/linux/udp.h"
10 #include "../include/linux/if_pppox.h"
11 
12 #include "../include/drv_types.h"
13 #include "../include/rtw_br_ext.h"
14 #include "../include/usb_osintf.h"
15 
16 #ifndef csum_ipv6_magic
17 #include "../include/net/ip6_checksum.h"
18 #endif
19 
20 #include "../include/linux/ipv6.h"
21 #include "../include/linux/icmpv6.h"
22 #include "../include/net/ndisc.h"
23 #include "../include/net/checksum.h"
24 
25 #define NAT25_IPV4		01
26 #define NAT25_IPV6		02
27 #define NAT25_IPX		03
28 #define NAT25_APPLE		04
29 #define NAT25_PPPOE		05
30 
31 #define RTL_RELAY_TAG_LEN (ETH_ALEN)
32 #define TAG_HDR_LEN		4
33 
34 #define MAGIC_CODE		0x8186
35 #define MAGIC_CODE_LEN	2
36 #define WAIT_TIME_PPPOE	5	/*  waiting time for pppoe server in sec */
37 
38 /*-----------------------------------------------------------------
39   How database records network address:
40 	   0    1    2    3    4    5    6    7    8    9   10
41 	|----|----|----|----|----|----|----|----|----|----|----|
42   IPv4  |type|                             |      IP addr      |
43   IPX   |type|      Net addr     |          Node addr          |
44   IPX   |type|      Net addr     |Sckt addr|
45   Apple |type| Network |node|
46   PPPoE |type|   SID   |           AC MAC            |
47 -----------------------------------------------------------------*/
48 
49 /* Find a tag in pppoe frame and return the pointer */
__nat25_find_pppoe_tag(struct pppoe_hdr * ph,unsigned short type)50 static unsigned char *__nat25_find_pppoe_tag(struct pppoe_hdr *ph, unsigned short type)
51 {
52 	unsigned char *cur_ptr, *start_ptr;
53 	unsigned short tagLen, tagType;
54 
55 	start_ptr = (unsigned char *)ph->tag;
56 	cur_ptr = (unsigned char *)ph->tag;
57 	while ((cur_ptr - start_ptr) < ntohs(ph->length)) {
58 		/*  prevent un-alignment access */
59 		tagType = (unsigned short)((cur_ptr[0] << 8) + cur_ptr[1]);
60 		tagLen  = (unsigned short)((cur_ptr[2] << 8) + cur_ptr[3]);
61 		if (tagType == type)
62 			return cur_ptr;
63 		cur_ptr = cur_ptr + TAG_HDR_LEN + tagLen;
64 	}
65 	return NULL;
66 }
67 
__nat25_add_pppoe_tag(struct sk_buff * skb,struct pppoe_tag * tag)68 static int __nat25_add_pppoe_tag(struct sk_buff *skb, struct pppoe_tag *tag)
69 {
70 	struct pppoe_hdr *ph = (struct pppoe_hdr *)(skb->data + ETH_HLEN);
71 	int data_len;
72 
73 	data_len = be16_to_cpu(tag->tag_len) + TAG_HDR_LEN;
74 	if (skb_tailroom(skb) < data_len)
75 		return -1;
76 
77 	skb_put(skb, data_len);
78 	/*  have a room for new tag */
79 	memmove(((unsigned char *)ph->tag + data_len), (unsigned char *)ph->tag, ntohs(ph->length));
80 	ph->length = htons(ntohs(ph->length) + data_len);
81 	memcpy((unsigned char *)ph->tag, tag, data_len);
82 	return data_len;
83 }
84 
skb_pull_and_merge(struct sk_buff * skb,unsigned char * src,int len)85 static int skb_pull_and_merge(struct sk_buff *skb, unsigned char *src, int len)
86 {
87 	int tail_len;
88 	unsigned long end, tail;
89 
90 	if ((src + len) > skb_tail_pointer(skb) || skb->len < len)
91 		return -1;
92 
93 	tail = (unsigned long)skb_tail_pointer(skb);
94 	end = (unsigned long)src + len;
95 	if (tail < end)
96 		return -1;
97 
98 	tail_len = (int)(tail - end);
99 	if (tail_len > 0)
100 		memmove(src, src + len, tail_len);
101 
102 	skb_trim(skb, skb->len - len);
103 	return 0;
104 }
105 
__nat25_has_expired(struct nat25_network_db_entry * fdb)106 static int  __nat25_has_expired(struct nat25_network_db_entry *fdb)
107 {
108 	if (time_before_eq(fdb->ageing_timer, jiffies - NAT25_AGEING_TIME * HZ))
109 		return 1;
110 
111 	return 0;
112 }
113 
__nat25_generate_ipv4_network_addr(unsigned char * networkAddr,unsigned int * ipAddr)114 static void __nat25_generate_ipv4_network_addr(unsigned char *networkAddr,
115 				unsigned int *ipAddr)
116 {
117 	memset(networkAddr, 0, MAX_NETWORK_ADDR_LEN);
118 
119 	networkAddr[0] = NAT25_IPV4;
120 	memcpy(networkAddr + 7, (unsigned char *)ipAddr, 4);
121 }
122 
__nat25_generate_pppoe_network_addr(unsigned char * networkAddr,unsigned char * ac_mac,__be16 * sid)123 static void __nat25_generate_pppoe_network_addr(unsigned char *networkAddr,
124 				unsigned char *ac_mac, __be16 *sid)
125 {
126 	memset(networkAddr, 0, MAX_NETWORK_ADDR_LEN);
127 
128 	networkAddr[0] = NAT25_PPPOE;
129 	memcpy(networkAddr + 1, (unsigned char *)sid, 2);
130 	memcpy(networkAddr + 3, (unsigned char *)ac_mac, 6);
131 }
132 
__nat25_generate_ipv6_network_addr(unsigned char * networkAddr,unsigned int * ipAddr)133 static  void __nat25_generate_ipv6_network_addr(unsigned char *networkAddr,
134 				unsigned int *ipAddr)
135 {
136 	memset(networkAddr, 0, MAX_NETWORK_ADDR_LEN);
137 
138 	networkAddr[0] = NAT25_IPV6;
139 	memcpy(networkAddr + 1, (unsigned char *)ipAddr, 16);
140 }
141 
scan_tlv(unsigned char * data,int len,unsigned char tag,unsigned char len8b)142 static unsigned char *scan_tlv(unsigned char *data, int len, unsigned char tag, unsigned char len8b)
143 {
144 	while (len > 0) {
145 		if (*data == tag && *(data + 1) == len8b && len >= len8b * 8)
146 			return data + 2;
147 
148 		len -= (*(data + 1)) * 8;
149 		data += (*(data + 1)) * 8;
150 	}
151 	return NULL;
152 }
153 
update_nd_link_layer_addr(unsigned char * data,int len,unsigned char * replace_mac)154 static int update_nd_link_layer_addr(unsigned char *data, int len, unsigned char *replace_mac)
155 {
156 	struct icmp6hdr *icmphdr = (struct icmp6hdr *)data;
157 	unsigned char *mac;
158 
159 	if (icmphdr->icmp6_type == NDISC_ROUTER_SOLICITATION) {
160 		if (len >= 8) {
161 			mac = scan_tlv(&data[8], len - 8, 1, 1);
162 			if (mac) {
163 				memcpy(mac, replace_mac, 6);
164 				return 1;
165 			}
166 		}
167 	} else if (icmphdr->icmp6_type == NDISC_ROUTER_ADVERTISEMENT) {
168 		if (len >= 16) {
169 			mac = scan_tlv(&data[16], len - 16, 1, 1);
170 			if (mac) {
171 				memcpy(mac, replace_mac, 6);
172 				return 1;
173 			}
174 		}
175 	} else if (icmphdr->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION) {
176 		if (len >= 24) {
177 			mac = scan_tlv(&data[24], len - 24, 1, 1);
178 			if (mac) {
179 				memcpy(mac, replace_mac, 6);
180 				return 1;
181 			}
182 		}
183 	} else if (icmphdr->icmp6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {
184 		if (len >= 24) {
185 			mac = scan_tlv(&data[24], len - 24, 2, 1);
186 			if (mac) {
187 				memcpy(mac, replace_mac, 6);
188 				return 1;
189 			}
190 		}
191 	} else if (icmphdr->icmp6_type == NDISC_REDIRECT) {
192 		if (len >= 40) {
193 			mac = scan_tlv(&data[40], len - 40, 2, 1);
194 			if (mac) {
195 				memcpy(mac, replace_mac, 6);
196 				return 1;
197 			}
198 		}
199 	}
200 	return 0;
201 }
202 
__nat25_network_hash(unsigned char * networkAddr)203 static int __nat25_network_hash(unsigned char *networkAddr)
204 {
205 	if (networkAddr[0] == NAT25_IPV4) {
206 		unsigned long x;
207 
208 		x = networkAddr[7] ^ networkAddr[8] ^ networkAddr[9] ^ networkAddr[10];
209 
210 		return x & (NAT25_HASH_SIZE - 1);
211 	} else if (networkAddr[0] == NAT25_IPX) {
212 		unsigned long x;
213 
214 		x = networkAddr[1] ^ networkAddr[2] ^ networkAddr[3] ^ networkAddr[4] ^ networkAddr[5] ^
215 			networkAddr[6] ^ networkAddr[7] ^ networkAddr[8] ^ networkAddr[9] ^ networkAddr[10];
216 
217 		return x & (NAT25_HASH_SIZE - 1);
218 	} else if (networkAddr[0] == NAT25_APPLE) {
219 		unsigned long x;
220 
221 		x = networkAddr[1] ^ networkAddr[2] ^ networkAddr[3];
222 
223 		return x & (NAT25_HASH_SIZE - 1);
224 	} else if (networkAddr[0] == NAT25_PPPOE) {
225 		unsigned long x;
226 
227 		x = networkAddr[0] ^ networkAddr[1] ^ networkAddr[2] ^ networkAddr[3] ^ networkAddr[4] ^ networkAddr[5] ^ networkAddr[6] ^ networkAddr[7] ^ networkAddr[8];
228 
229 		return x & (NAT25_HASH_SIZE - 1);
230 	} else if (networkAddr[0] == NAT25_IPV6) {
231 		unsigned long x;
232 
233 		x = networkAddr[1] ^ networkAddr[2] ^ networkAddr[3] ^ networkAddr[4] ^ networkAddr[5] ^
234 			networkAddr[6] ^ networkAddr[7] ^ networkAddr[8] ^ networkAddr[9] ^ networkAddr[10] ^
235 			networkAddr[11] ^ networkAddr[12] ^ networkAddr[13] ^ networkAddr[14] ^ networkAddr[15] ^
236 			networkAddr[16];
237 
238 		return x & (NAT25_HASH_SIZE - 1);
239 	} else {
240 		unsigned long x = 0;
241 		int i;
242 
243 		for (i = 0; i < MAX_NETWORK_ADDR_LEN; i++)
244 			x ^= networkAddr[i];
245 
246 		return x & (NAT25_HASH_SIZE - 1);
247 	}
248 }
249 
__network_hash_link(struct adapter * priv,struct nat25_network_db_entry * ent,int hash)250 static void __network_hash_link(struct adapter *priv,
251 				struct nat25_network_db_entry *ent, int hash)
252 {
253 	/*  Caller must spin_lock already! */
254 	ent->next_hash = priv->nethash[hash];
255 	if (ent->next_hash)
256 		ent->next_hash->pprev_hash = &ent->next_hash;
257 	priv->nethash[hash] = ent;
258 	ent->pprev_hash = &priv->nethash[hash];
259 }
260 
__network_hash_unlink(struct nat25_network_db_entry * ent)261 static void __network_hash_unlink(struct nat25_network_db_entry *ent)
262 {
263 	/*  Caller must spin_lock already! */
264 	*ent->pprev_hash = ent->next_hash;
265 	if (ent->next_hash)
266 		ent->next_hash->pprev_hash = ent->pprev_hash;
267 	ent->next_hash = NULL;
268 	ent->pprev_hash = NULL;
269 }
270 
__nat25_db_network_insert(struct adapter * priv,unsigned char * macAddr,unsigned char * networkAddr)271 static void __nat25_db_network_insert(struct adapter *priv,
272 				unsigned char *macAddr, unsigned char *networkAddr)
273 {
274 	struct nat25_network_db_entry *db;
275 	int hash;
276 
277 	spin_lock_bh(&priv->br_ext_lock);
278 	hash = __nat25_network_hash(networkAddr);
279 	db = priv->nethash[hash];
280 	while (db) {
281 		if (!memcmp(db->networkAddr, networkAddr, MAX_NETWORK_ADDR_LEN)) {
282 			memcpy(db->macAddr, macAddr, ETH_ALEN);
283 			db->ageing_timer = jiffies;
284 			spin_unlock_bh(&priv->br_ext_lock);
285 			return;
286 		}
287 		db = db->next_hash;
288 	}
289 	db = kmalloc(sizeof(*db), GFP_ATOMIC);
290 	if (!db) {
291 		spin_unlock_bh(&priv->br_ext_lock);
292 		return;
293 	}
294 	memcpy(db->networkAddr, networkAddr, MAX_NETWORK_ADDR_LEN);
295 	memcpy(db->macAddr, macAddr, ETH_ALEN);
296 	atomic_set(&db->use_count, 1);
297 	db->ageing_timer = jiffies;
298 
299 	__network_hash_link(priv, db, hash);
300 
301 	spin_unlock_bh(&priv->br_ext_lock);
302 }
303 
304 /*
305  *	NAT2.5 interface
306  */
307 
nat25_db_cleanup(struct adapter * priv)308 void nat25_db_cleanup(struct adapter *priv)
309 {
310 	int i;
311 
312 	spin_lock_bh(&priv->br_ext_lock);
313 
314 	for (i = 0; i < NAT25_HASH_SIZE; i++) {
315 		struct nat25_network_db_entry *f;
316 
317 		f = priv->nethash[i];
318 		while (f) {
319 			struct nat25_network_db_entry *g;
320 
321 			g = f->next_hash;
322 			if (priv->scdb_entry == f) {
323 				memset(priv->scdb_mac, 0, ETH_ALEN);
324 				memset(priv->scdb_ip, 0, 4);
325 				priv->scdb_entry = NULL;
326 			}
327 			__network_hash_unlink(f);
328 			kfree(f);
329 			f = g;
330 		}
331 	}
332 	spin_unlock_bh(&priv->br_ext_lock);
333 }
334 
nat25_db_expire(struct adapter * priv)335 void nat25_db_expire(struct adapter *priv)
336 {
337 	int i;
338 
339 	spin_lock_bh(&priv->br_ext_lock);
340 
341 	for (i = 0; i < NAT25_HASH_SIZE; i++) {
342 		struct nat25_network_db_entry *f;
343 
344 		f = priv->nethash[i];
345 		while (f) {
346 			struct nat25_network_db_entry *g;
347 
348 			g = f->next_hash;
349 			if (__nat25_has_expired(f)) {
350 				if (atomic_dec_and_test(&f->use_count)) {
351 					if (priv->scdb_entry == f) {
352 						memset(priv->scdb_mac, 0, ETH_ALEN);
353 						memset(priv->scdb_ip, 0, 4);
354 						priv->scdb_entry = NULL;
355 					}
356 					__network_hash_unlink(f);
357 					kfree(f);
358 				}
359 			}
360 			f = g;
361 		}
362 	}
363 	spin_unlock_bh(&priv->br_ext_lock);
364 }
365 
nat25_db_handle(struct adapter * priv,struct sk_buff * skb,int method)366 int nat25_db_handle(struct adapter *priv, struct sk_buff *skb, int method)
367 {
368 	unsigned short protocol;
369 	unsigned char networkAddr[MAX_NETWORK_ADDR_LEN];
370 	unsigned int tmp;
371 
372 	if (!skb)
373 		return -1;
374 
375 	if ((method <= NAT25_MIN) || (method >= NAT25_MAX))
376 		return -1;
377 
378 	protocol = be16_to_cpu(*((__be16 *)(skb->data + 2 * ETH_ALEN)));
379 
380 	/*---------------------------------------------------*/
381 	/*                 Handle IP frame                   */
382 	/*---------------------------------------------------*/
383 	if (protocol == ETH_P_IP) {
384 		struct iphdr *iph = (struct iphdr *)(skb->data + ETH_HLEN);
385 
386 		if (((unsigned char *)(iph) + (iph->ihl << 2)) >= (skb->data + ETH_HLEN + skb->len))
387 			return -1;
388 
389 		switch (method) {
390 		case NAT25_CHECK:
391 			return -1;
392 		case NAT25_INSERT:
393 			/* some multicast with source IP is all zero, maybe other case is illegal */
394 			/* in class A, B, C, host address is all zero or all one is illegal */
395 			if (iph->saddr == 0)
396 				return 0;
397 			tmp = be32_to_cpu(iph->saddr);
398 			__nat25_generate_ipv4_network_addr(networkAddr, &tmp);
399 			/* record source IP address and , source mac address into db */
400 			__nat25_db_network_insert(priv, skb->data + ETH_ALEN, networkAddr);
401 			return 0;
402 		default:
403 			return -1;
404 		}
405 	} else if (protocol == ETH_P_ARP) {
406 		/*---------------------------------------------------*/
407 		/*                 Handle ARP frame                  */
408 		/*---------------------------------------------------*/
409 		struct arphdr *arp = (struct arphdr *)(skb->data + ETH_HLEN);
410 		unsigned char *arp_ptr = (unsigned char *)(arp + 1);
411 		unsigned int *sender;
412 
413 		if (arp->ar_pro != htons(ETH_P_IP))
414 			return -1;
415 
416 		switch (method) {
417 		case NAT25_CHECK:
418 			return 0;	/*  skb_copy for all ARP frame */
419 		case NAT25_INSERT:
420 			/*  change to ARP sender mac address to wlan STA address */
421 			memcpy(arp_ptr, GET_MY_HWADDR(priv), ETH_ALEN);
422 			arp_ptr += arp->ar_hln;
423 			sender = (unsigned int *)arp_ptr;
424 			__nat25_generate_ipv4_network_addr(networkAddr, sender);
425 			__nat25_db_network_insert(priv, skb->data + ETH_ALEN, networkAddr);
426 			return 0;
427 		default:
428 			return -1;
429 		}
430 	} else if ((protocol == ETH_P_PPP_DISC) ||
431 		   (protocol == ETH_P_PPP_SES)) {
432 		/*---------------------------------------------------*/
433 		/*                Handle PPPoE frame                 */
434 		/*---------------------------------------------------*/
435 		struct pppoe_hdr *ph = (struct pppoe_hdr *)(skb->data + ETH_HLEN);
436 		__be16 *pMagic;
437 
438 		switch (method) {
439 		case NAT25_CHECK:
440 			if (ph->sid == 0)
441 				return 0;
442 			return 1;
443 		case NAT25_INSERT:
444 			if (ph->sid == 0) {	/*  Discovery phase according to tag */
445 				if (ph->code == PADI_CODE || ph->code == PADR_CODE) {
446 					if (priv->ethBrExtInfo.addPPPoETag) {
447 						struct pppoe_tag *tag, *pOldTag;
448 						unsigned char tag_buf[40];
449 						int old_tag_len = 0;
450 
451 						tag = (struct pppoe_tag *)tag_buf;
452 						pOldTag = (struct pppoe_tag *)__nat25_find_pppoe_tag(ph, ntohs(PTT_RELAY_SID));
453 						if (pOldTag) { /*  if SID existed, copy old value and delete it */
454 							old_tag_len = ntohs(pOldTag->tag_len);
455 							if (old_tag_len +
456 							    TAG_HDR_LEN +
457 							    MAGIC_CODE_LEN +
458 							    RTL_RELAY_TAG_LEN >
459 							    sizeof(tag_buf))
460 								return -1;
461 
462 							memcpy(tag->tag_data + MAGIC_CODE_LEN + RTL_RELAY_TAG_LEN,
463 								pOldTag->tag_data, old_tag_len);
464 
465 							if (skb_pull_and_merge(skb, (unsigned char *)pOldTag, TAG_HDR_LEN + old_tag_len) < 0)
466 								return -1;
467 
468 							ph->length = htons(ntohs(ph->length) - TAG_HDR_LEN - old_tag_len);
469 						}
470 
471 						tag->tag_type = PTT_RELAY_SID;
472 						tag->tag_len = htons(MAGIC_CODE_LEN + RTL_RELAY_TAG_LEN + old_tag_len);
473 
474 						/*  insert the magic_code+client mac in relay tag */
475 						pMagic = (__be16 *)tag->tag_data;
476 						*pMagic = htons(MAGIC_CODE);
477 						memcpy(tag->tag_data + MAGIC_CODE_LEN, skb->data + ETH_ALEN, ETH_ALEN);
478 
479 						/* Add relay tag */
480 						if (__nat25_add_pppoe_tag(skb, tag) < 0)
481 							return -1;
482 					} else { /*  not add relay tag */
483 						if (priv->pppoe_connection_in_progress &&
484 						    memcmp(skb->data + ETH_ALEN,
485 							   priv->pppoe_addr,
486 							   ETH_ALEN))
487 							return -2;
488 
489 						if (priv->pppoe_connection_in_progress == 0)
490 							memcpy(priv->pppoe_addr, skb->data + ETH_ALEN, ETH_ALEN);
491 
492 						priv->pppoe_connection_in_progress = WAIT_TIME_PPPOE;
493 					}
494 				} else {
495 					return -1;
496 				}
497 			} else {	/*  session phase */
498 				__nat25_generate_pppoe_network_addr(networkAddr, skb->data, &ph->sid);
499 
500 				__nat25_db_network_insert(priv, skb->data + ETH_ALEN, networkAddr);
501 
502 				if (!priv->ethBrExtInfo.addPPPoETag &&
503 				    priv->pppoe_connection_in_progress &&
504 				    !memcmp(skb->data + ETH_ALEN, priv->pppoe_addr, ETH_ALEN))
505 					priv->pppoe_connection_in_progress = 0;
506 			}
507 			return 0;
508 		default:
509 			return -1;
510 		}
511 	} else if (protocol == 0x888e) {
512 		/*---------------------------------------------------*/
513 		/*                 Handle EAP frame                  */
514 		/*---------------------------------------------------*/
515 		switch (method) {
516 		case NAT25_CHECK:
517 			return -1;
518 		case NAT25_INSERT:
519 			return 0;
520 		default:
521 			return -1;
522 		}
523 	} else if ((protocol == 0xe2ae) || (protocol == 0xe2af)) {
524 		/*---------------------------------------------------*/
525 		/*         Handle C-Media proprietary frame          */
526 		/*---------------------------------------------------*/
527 		switch (method) {
528 		case NAT25_CHECK:
529 			return -1;
530 		case NAT25_INSERT:
531 			return 0;
532 		default:
533 			return -1;
534 		}
535 	} else if (protocol == ETH_P_IPV6) {
536 		/*------------------------------------------------*/
537 		/*         Handle IPV6 frame			  */
538 		/*------------------------------------------------*/
539 		struct ipv6hdr *iph = (struct ipv6hdr *)(skb->data + ETH_HLEN);
540 
541 		if (sizeof(*iph) >= (skb->len - ETH_HLEN))
542 			return -1;
543 
544 		switch (method) {
545 		case NAT25_CHECK:
546 			if (skb->data[0] & 1)
547 				return 0;
548 			return -1;
549 		case NAT25_INSERT:
550 			if (memcmp(&iph->saddr, "\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0", 16)) {
551 				__nat25_generate_ipv6_network_addr(networkAddr, (unsigned int *)&iph->saddr);
552 				__nat25_db_network_insert(priv, skb->data + ETH_ALEN, networkAddr);
553 
554 				if (iph->nexthdr == IPPROTO_ICMPV6 &&
555 						skb->len > (ETH_HLEN +  sizeof(*iph) + 4)) {
556 					if (update_nd_link_layer_addr(skb->data + ETH_HLEN + sizeof(*iph),
557 								      skb->len - ETH_HLEN - sizeof(*iph), GET_MY_HWADDR(priv))) {
558 						struct icmp6hdr  *hdr = (struct icmp6hdr *)(skb->data + ETH_HLEN + sizeof(*iph));
559 						hdr->icmp6_cksum = 0;
560 						hdr->icmp6_cksum = csum_ipv6_magic(&iph->saddr, &iph->daddr,
561 										be16_to_cpu(iph->payload_len),
562 										IPPROTO_ICMPV6,
563 										csum_partial((__u8 *)hdr,
564 										be16_to_cpu(iph->payload_len),
565 										0));
566 					}
567 				}
568 			}
569 			return 0;
570 		default:
571 			return -1;
572 		}
573 	}
574 	return -1;
575 }
576 
577 #define SERVER_PORT			67
578 #define CLIENT_PORT			68
579 #define DHCP_MAGIC			0x63825363
580 #define BROADCAST_FLAG		0x8000
581 
582 struct dhcpMessage {
583 	u_int8_t op;
584 	u_int8_t htype;
585 	u_int8_t hlen;
586 	u_int8_t hops;
587 	u_int32_t xid;
588 	__be16 secs;
589 	__be16 flags;
590 	__be32 ciaddr;
591 	__be32 yiaddr;
592 	__be32 siaddr;
593 	__be32 giaddr;
594 	u_int8_t chaddr[16];
595 	u_int8_t sname[64];
596 	u_int8_t file[128];
597 	__be32 cookie;
598 	u_int8_t options[308]; /* 312 - cookie */
599 };
600 
dhcp_flag_bcast(struct adapter * priv,struct sk_buff * skb)601 void dhcp_flag_bcast(struct adapter *priv, struct sk_buff *skb)
602 {
603 	if (!skb)
604 		return;
605 
606 	if (!priv->ethBrExtInfo.dhcp_bcst_disable) {
607 		__be16 protocol = *((__be16 *)(skb->data + 2 * ETH_ALEN));
608 
609 		if (protocol == __constant_htons(ETH_P_IP)) { /*  IP */
610 			struct iphdr *iph = (struct iphdr *)(skb->data + ETH_HLEN);
611 
612 			if (iph->protocol == IPPROTO_UDP) { /*  UDP */
613 				struct udphdr *udph = (struct udphdr *)((size_t)iph + (iph->ihl << 2));
614 
615 				if ((udph->source == __constant_htons(CLIENT_PORT)) &&
616 				    (udph->dest == __constant_htons(SERVER_PORT))) { /*  DHCP request */
617 					struct dhcpMessage *dhcph =
618 						(struct dhcpMessage *)((size_t)udph + sizeof(struct udphdr));
619 					u32 cookie = be32_to_cpu((__be32)dhcph->cookie);
620 
621 					if (cookie == DHCP_MAGIC) { /*  match magic word */
622 						if (!(dhcph->flags & htons(BROADCAST_FLAG))) {
623 							/*  if not broadcast */
624 							register int sum = 0;
625 
626 							/*  or BROADCAST flag */
627 							dhcph->flags |= htons(BROADCAST_FLAG);
628 							/*  recalculate checksum */
629 							sum = ~(udph->check) & 0xffff;
630 							sum += be16_to_cpu(dhcph->flags);
631 							while (sum >> 16)
632 								sum = (sum & 0xffff) + (sum >> 16);
633 							udph->check = ~sum;
634 						}
635 					}
636 				}
637 			}
638 		}
639 	}
640 }
641 
scdb_findEntry(struct adapter * priv,unsigned char * ipAddr)642 void *scdb_findEntry(struct adapter *priv, unsigned char *ipAddr)
643 {
644 	unsigned char networkAddr[MAX_NETWORK_ADDR_LEN];
645 	struct nat25_network_db_entry *db;
646 	int hash;
647 
648 	__nat25_generate_ipv4_network_addr(networkAddr, (unsigned int *)ipAddr);
649 	hash = __nat25_network_hash(networkAddr);
650 	db = priv->nethash[hash];
651 	while (db) {
652 		if (!memcmp(db->networkAddr, networkAddr, MAX_NETWORK_ADDR_LEN)) {
653 			return (void *)db;
654 		}
655 
656 		db = db->next_hash;
657 	}
658 
659 	return NULL;
660 }
661